MySQL select from text column with specification - mysql

I would like to query a database which is MySQL 5.
Let's say database name is db and the table name is table and the column name is column
and that column is a text
containing the following for example:
aksksksksjsjk&ct=100&lor=10
aksksksksjsjk&ct=1001001001001001&lor=10
So i would like to query that table and grep only where ct start with number 1 and it's 16 numbers.
I tried with SELECT column FROM db.table WHERE column LIKE '%ct=1%'
so it's gonna grep where ct start with number 1
so kindly try to help me to proceed with select ct when start with number 1 and contain 16 numbers

In its basic form, you might want to use
SELECT column FROM db.table WHERE column REGEXP BINARY 'ct=1[0-9]{15}'
Or, to match as a whole word:
SELECT column FROM db.table WHERE column REGEXP BINARY '[[:<:]]ct=1[0-9]{15}[[:>:]]'
Note that [0-9]{15} matches 15 digits.
The BINARY keyword will make matching case sensitive, so only ct will get matched and CT won't. Remove it if you need to keep the regex case insensitive.
The [[:<:]] matches the left-hand (starting) word boundary and [[:>:]] matches the trailing (end) word boundary.

Related

Length() is counting \0 as a character

I have a MySQL table that has a column named Description. The column is of size varchar(100) with default as NULL.
I want to select from the table where length(Description)>0.
There are some rows where Description is empty, but when I check the length it returns 1.
I copied the row from MySQL workbench and pasted it on Notepad++, and I see that the empty Description column has \0 in it and the length is 1.
How can I NOT count \0 as a character?
I have tried these:
select * from MyTable where length(trim(Description))>0; -->> doesnt work. length is 1 for empty column
select * from MyTable where Description<>''; -->> doesnt work. length is 1 for empty column
select * from MyTable where length(trim(ifnull(Description,'')))>0; -->> doesnt work. length is 1 for empty column.
length(replace(Description,'\0',''))
Often people ask the opposite question, how to count the number of occurrences of a string; you use length for that too:
(length(Description) - length(replace(Description,somestring,''))) / length(somestring)
The \0 represents the ASCII null character. One approach here would be to check the length of the column after removing it:
SELECT *
FROM MyTable
WHERE LENGTH(REPLACE(TRIM(Description), CHAR(0x00 using utf8), '')) > 0;

MySQL command to get first letter of last name

Hello I have made a dummy table that I am practicing with and I am trying to get the lasts name first letter for example. Aba Kadabra and Alfa Kadabra the last letter of their last name is 'K' so when I was testing some queries such as...
select * from employees
where full_name like 'K%'
select * from employees
where full_name like 'K%'
Neither of these worked. Can anyone tell me the best way to accomplish this?
Because % works that way. See here
So, 'K%' just brings all full_name that start with K.
and '%K' brings all full_name that end with K.
What you need is '% K%', test it please.
MySQL LIKE operator checks whether a specific character string matches
a specified pattern.
The LIKE operator does a pattern matching comparison. The operand to
the right of the LIKE operator contains the pattern and the left hand
operand contains the string to match against the pattern. A percent
symbol ("%") in the LIKE pattern matches any sequence of zero or more
characters in the string. An underscore ("_") in the LIKE pattern
matches any single character in the string. Any other character
matches itself or its lower/upper case equivalent (i.e.
case-insensitive matching). (A bug: SQLite only understands
upper/lower case for ASCII characters by default. The LIKE operator is
case sensitive by default for unicode characters that are beyond the
ASCII range. For example, the expression 'a' LIKE 'A' is TRUE but 'æ'
LIKE 'Æ' is FALSE.)
You can use below query:
select * from table where full_name like '% K%'

Is it possible in MySQL to search a table for where a column only contains 1 comma?

I have this column in a table which is comma delimited to separate the values.
Here's the sample data:
2003,2004
2003,2005
2003,2006
2003,2004,2005
2003,2007
I want to get all data that contains only 1 comma.
I've been playing around with the '%' and '_' wildcards, but I can't seem to get the results I need.
SELECT column FROM table WHERE column like '%_,%'
Replace the , with '' empty set then take the original length less the replaced length. if 1 then only 1 comma if > 1 then more than 1 comma.
The length difference would represent the number of commas.
Length(column) - length(Replace(column,',','')) as NumOfCommas
or
where Length(column) - length(Replace(column,',','')) =1
While this may solve the problem, I agree with what others have indicated. Storing multiple values in a single column in a RDBMS is asking for more trouble. Better to normalize the data and get it to at least 3rd Normal form!
You can also use find_in_set() method which searches a value in comma separated list, by picking the last value of column using substring_index we can then check result of find_in_set should be 2 so that its the second and last value from list
select *
from demo
where find_in_set(substring_index(data,',',-1),data) = 2
Demo
Maybe another solution is to use regular expression in your case it can look like this ^[0-9]{4},[0-9]{4}$ :
SELECT * FROM MyTable WHERE ColName REGEXP '^[0-9]{4},[0-9]{4}$'
Or if you want all non comma one or more time :
SELECT * FROM MyTable WHERE ColName REGEXP '^[^,]*,[^,]*$'

Mysql REGEXP select numbers starting with given digit(s)

my table has a column with comma-separated (and eventually a space, too) numbers; those numbers can have from five to twelve digits.
9645811, 9646011,9645911, 9646111
or
41031, 41027, 559645811, 5501006009
I need to select the rows with that column containing a number STARTING with given digits. In the above examples, only the first has to be selected. What I've tried so far:
SELECT myfield FROM mytable
WHERE myfield REGEXP ('(^|[,\s]+)(96458[\d]*)([,\s]*|$)');
However the query returns no results. I'd like to select only the first row, where there is a number STARTING with 96458.
Any help would be appreciated :)
You need to use a starting word boundary [[:<:]]:
SELECT myfield FROM mytable WHERE myfield REGEXP ('[[:<:]]96458');
See the MySQL regex syntax for more details.
[[:<:]], [[:>:]]
These markers stand for word boundaries. They match the beginning and end of words, respectively.
See this SQL fiddle.

REGEXP - Select only rows that contain letters and full stop

I've been trying to write this query, I need to select the rows where a column has only letters (a-z) and a full stop.
I tried this but it's not working:
SELECT * FROM table WHERE (c1 REGEXP '[^a-zA-Z\.]') = 0
This one would usually work in PHP.
Try:
SELECT * FROM table WHERE c1 REGEXP '^[a-zA-Z.]+$'
The anchor ^ and $ ensure that you are matching the entire string and not part of it. Next the character class [a-zA-Z.] matches a single upper/lower case letter or a period. The + is the quantifier for one or more repetitions of the previous sub-regex, so in this case it allows us to match one or more of either a period or a upper/lower case letter.
More info on regex usage in MySQL