I have a result like this below.
212.212.212.212,
212.212.212.211,
212.212.212.213, 10.16.10.10,
212.212.212.215,
How can i just select only
212.212.212.213,
212.212.212.213,
212.212.212.215,
with mysql regexp ?
select numbers from table where numbers REGEXP ', $'
That gives all.
Not: there is space after each comma.
select numbers from table where numbers REGEXP '^[^,]+, $'
But really, you should not store comma-separated values in a database table in the first place.
try
select numbers from table where numbers REGEXP '^[^,]+, $'
#Tomalak You're very right
Related
I have a table with a column having values like:
AB123
AB209
ABQ52
AB18C
I would like to extract rows whose last three characters are numbers. How can I do this?
The original table is more complicated, and I tried the "WHERE" clause with "AB___", which returned the above to me.
You can use a combination of SUBSTRING and REGEXP like this:
SELECT yourcolumn FROM yourtable WHERE SUBSTRING(yourcolumn, -3) REGEXP '^[0-9]+$';
The SUBSTRING part will cut the last 3 characters of the column's value and the REGEXP condition will check whether this substring is numeric.
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'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
How to get records which contain alphaNumeric characters + white-spaces.
OR At-list single numeric character in name.
i.e spiderman 1, abc12 part1
What I have done.:
select * from table t where t.name REGEXP '^[A-Za-z0-9]+$'
but it will gives only records which dont have white space : i.e abc123
so I also tried
select * from table t where t.name REGEXP '^[A-Za-z0-9 ]+$'
Now, it gives me some records which does not contain any numeric characters. i.e abcdefg hij
SELECT *
FROM table
WHERE name REGEXP '^[a-zA-Z0-9 ]+$' AND name REGEXP '[0-9]'
Simpler:
SELECT *
FROM table
WHERE name REGEXP '^[a-zA-Z0-9 ]*[0-9][a-zA-Z0-9 ]*$'
What about this: ^[\w\s]*$, which will match any numbers or word characters or whitespaces.
A digit may appear in the start, middle, or end. So we have to take care of these combinations too for which regex groups are used, like this:
select *
from table t
where t.name REGEXP '^[a-zA-Z ]+[0-9 ]+[a-zA-Z ]*$|^[0-9]+[a-zA-Z ]+[0-9]*$'
I have values stored like this in a field 1,255,230,265.
Is there a function in MySQL that will give me the second value in that string? In this case it'll be 255.
I tried using locate, but that does not seem to be meant for this.
Try this
select SUBSTRING_INDEX(SUBSTRING_INDEX(field_name,',',2),",",-1) from table_name
You might want to use SUBSTRING_INDEX() function.
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(field,',',2),',',-1)
FROM yourTable.
This grabs everything infront of the second comma, then grabs everything after the last comma (-1)
Try This
select * from Table1 where ',' + Nos+ ',' like '%,255,%'
Refer:
MySQL query finding values in a comma separated string
mysql check if numbers are in a comma separated list
Use FIND_IN_SET() function:
SELECT *
FROM tableA
WHERE FIND_IN_SET(255, columnName)
OR
Use LIKE operator
SELECT *
FROM tableA
WHERE CONCAT(',', columnName, ',') LIKE '%,255,%'