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;
Related
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 '^[^,]*,[^,]*$'
I have data set of about 10K alphanumeric words with 10 characters length each. I need to match these using the first 3 characters and the last 3 characters.
Example: BGP12BR2010
In this case, I should use only BGP and 010 and see if there are any entries in my database. I have used
LEFT(replace(term_id,' ',''),3)||RIGHT(replace(term_id,' ',''),3)
Is there any other way to get this done.
You can also use LIKE:
SELECT * FROM yourTabel WHERE term_id LIKE 'BGP%210';
this matches on all string, not only 10 CHAR. to specify the lenght you can
use underscore
SELECT * FROM yourTabel WHERE term_id LIKE 'BGP____210';
A better way for this is to add 2 virtual persitent fields, where Mysql calculate the values and you also can set a index on it for a better performance and not using a full table scan
add persistent virtual fields
ALTER TABLE yourtable
ADD COLUMN first3 VARCHAR(5) AS (SUBSTRING('hallo',1,3)) PERSISTENT,
ADD COLUMN last3 VARCHAR(5) AS (SUBSTRING('hallo',-3,3)) PERSISTENT;
Now you can select it
SELECT * FROM yourTable where first in('BGP','YXZ','XXX) and last3 = '210';
I'll do so:
SELECT * FROM yourtable
WHERE LENGTH(yourcolumn) = 10
AND yourcolumn LIKE 'BPG%010';
To get all the values starting with 3 alphabets and ending with 3 numeric characters, use
select *
from t
where val regexp '^[a-z]{3}.+[0-9]{3}$'
To extract them, if they follow the above pattern,
select val, substring(val,1,3) as first3, substring(val,-3,3) last3,
--concatenate them if required
concat(substring(val,1,3), substring(val,-3,3)) concatenated_string
from t
where val regexp '^[a-z]{3}.+[0-9]{3}$'
Add a condition for length of the column if it has to be exactly 10 characters. In that case, change the regexp to '^[a-z]{3}.{numcharactersrequired}[0-9]{3}$' , which would be '^[a-z]{3}.{4}[0-9]{3}$'
SQL Fiddle
I'm trying to select all rows that contain only alphanumeric characters in MySQL using:
SELECT * FROM table WHERE column REGEXP '[A-Za-z0-9]';
However, it's returning all rows, regardless of the fact that they contain non-alphanumeric characters.
Try this code:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$'
This makes sure that all characters match.
Your statement matches any string that contains a letter or digit anywhere, even if it contains other non-alphanumeric characters. Try this:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$';
^ and $ require the entire string to match rather than just any portion of it, and + looks for 1 or more alphanumberic characters.
You could also use a named character class if you prefer:
SELECT * FROM table WHERE column REGEXP '^[[:alnum:]]+$';
Try this:
REGEXP '^[a-z0-9]+$'
As regexp is not case sensitive except for binary fields.
There is also this:
select m from table where not regexp_like(m, '^[0-9]\d+$')
which selects the rows that contains characters from the column you want (which is m in the example but you can change).
Most of the combinations don't work properly in Oracle platforms but this does. Sharing for future reference.
Try this
select count(*) from table where cast(col as double) is null;
Change the REGEXP to Like
SELECT * FROM table_name WHERE column_name like '%[^a-zA-Z0-9]%'
this one works fine
I need a SELECT query in MYSQL that will retrieve all rows in one table witch field values contain "?" char with one condition: the char is not the last character
Example:
ID Field
1 123??see
2 12?
3 45??78??
Returning rows would then be those from ID 1 and 3 that match the condition given
The only statement I have is:
SELECT *
FROM table
WHERE Field LIKE '%?%'
But, the MySQL query does not solve my problem..
The LIKE expressions also support a wildcard "_" which matches exactly one character.
So you can write an expression like the example below, and know that your "?" will not be the last character in the string. There must be at least one more character.
WHERE intrebare LIKE '%?_%'
Re comment from #JohnRuddell,
Yes, that's true, this will match the string "??" because a "?" exists in a position that is not the last character.
It depends whether the OP means for that to be a match or not. The OP says the string "45??78??" is a match, but it's not clear if they would intend that "4578??" to be a match.
An alternative is to use a regular expression, but this is a little more tricky because you have to escape a literal "?", so it won't be interpreted as a regexp metacharacter. Then also escape the escape character.
WHERE intrebare REGEXP '\\?[^?]'
you can just add an additional where where the last character is not a ?
SELECT *
FROM intrebari
WHERE intrebare LIKE '%?%' AND intrebare NOT LIKE '%?'
you could also do it like this
SELECT *
FROM intrebari
WHERE intrebare LIKE '%?%' AND RIGHT(intrebare,1) <> '?'
DEMO
I want to fetch all records which has one column contained % sign in mysql
we can do this using mysql using like
for ex..
select * from table where column like '%%';
it returns all records..
Please suggest
Use a backslash to escape the percent:
select * from table where column like '%\%%';
will match any row containing a percent character
% is a special character, try using escape characters to find it. Right now you're just telling mysql to look for a string using 2 wildcard characters (%) as opposed to the actual '%' character. Try using
select * from table where column = 'a%' ESCAPE 'a'
Basically telling mySQL to "Look for the string 'a%', but remove the char a in front of it.
EDIT: Another option is just using
select * from table where column = '\%'
Doing the same thing on later mySQL versions. The backslash is the "standard" escape character.
EDIT 2: Or to actually answer your question:
select * from table where column = '%\%%'
You need to escape the literal % sign with a \ e.g.
select * from table where column like '\%%';