How to select data where the last three characters are numbers? - mysql

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.

Related

Column that starts with AST and ends in only number

I suck at REGEX, but I need to pull all the records from a table column that stats with AST, and the rest only contains numbers after. I am assuming this can be done with just REGEX and not LIKE but I'm not sure.
For instance AST000001
and not AST99XXH011
SELECT * FROM table WHERE column LIKE 'AST%' AND column REGEXP '[0-9]$'
You can use REGEXP/RLIKE on the whole column value (using start-of-string (^) and end-of-string ($) anchors to ensure you match the entire column):
SELECT *
FROM `table`
WHERE `column` REGEXP '^AST[0-9]+$'
Demo on dbfiddle

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 SELECT all rows where char exists in value but not the last one

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

MySQL REGEXP not matching with 2 commas

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

How to query comma delimited field of table to see if number is within the field

I want to select rows from a table if a column contains a certain number e.g 981. The data in these columns is either 0, null or in the format below:
1007,1035,1189,908,977,974,979,973,982,981,1007
How do I phrase this in a query?
Obvious this query below isn't sufficient but I need something similar
SELECT * FROM `table` WHERE `column`='981'
Thanks
If you have regular expressions it may work:
SELECT * FROM `table` WHERE `column` REGEXP '(^|,)951($|,)'
You can use the IN clause which checks whether a value is within a set of values.
SELECT * FROM `table` WHERE `column` IN (1007,1035,1189,979,973,982,981,1007)
or
SELECT * FROM `table` WHERE `column` IN ('abc','def','ghi')
Please note that cannot mix quoted and unquoted values in the IN list
EDIT
I misunderstood the original question. If your column data is like 1007,1035,1189,979,973,982,981,1007 and you're searching for the presence of 981 then you'll have to use LIKE instead of IN
SELECT * FROM table WHERE (column LIKE ('%,981,%') OR column LIKE ('%,981') OR column LIKE ('981,%'))
to pattern match the value 981 in the middle, at the end or at the beginning of those comma separated values.