I need to write a query using MYSQL REGEXP that will find me rows that have a certain column begin with 11 or 12 or etc. I know I can use LIKE or LEFT(####,2) but would like to use the REGEXP option. My data is stored as 110001, 122122, 130013a and etc.
EDIT 1:
To make it more clear, I would like to express
SELECT * FROM table WHERE column LIKE '11%' or column LIKE '12%' or column LIKE '30%'"
with REGEXP
Thanks!
To match rows that start with any two digits you can use:
SELECT *
FROM yourtable
WHERE yourcolumn REGEXP '^[0-9][0-9]';
If you only want to allow rows starting with 11, 12 or 30 then you could use this:
SELECT *
FROM yourtable
WHERE yourcolumn REGEXP '^(11|12|30)';
This will not be able to use an index on the column so it may be slower than using LIKE.
Related
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 want to find all rows in which a column contains only digits.
The first idea was using LIKE [0-9]%, but in fact this matches any strings starting with a digit.
It seems to be a very simple task, but unfortunately I can't find solution
In Sql server use Not Like
where some_column NOT LIKE '%[^0-9]%'
Demo
declare #str varchar(50)='asdarew345'
select 1 where #str NOT LIKE '%[^0-9]%'
SQL FIDDLE DEMO
For Mysql use REGEX
SELECT *
FROM yourtable
WHERE string_column REGEXP '^[0-9]+$'
In MySQL we can use this code to select rows with ID numbers (or anything else) between a list:
SELECT * FROM TABLENAME WHERE prophrases IN (1,2,3,4,5,6)
Thats OK! but if we need to search a number in a Field's value, what we can do?
For example, I have a table with a field, named 'prophases' and I saved data like this:
rowid / prophases
1 / 1,2,3,4,5,6,7,8
2 / 6,5,2,7,9,2
now, i need to check if a number like 6 is in prophrases in row #1 or not!
what can i do for that?
something like this but in correct form!
SELECT * FROM TABLENAME WHERE 6 IN prophrases
you should just use FIND_IN_SET()
SELECT rowid
FROM TABLENAME
WHERE FIND_IN_SET('6', prophrases)
This will work if you are only dealing with single digits
select * from tablename where prophrases like '%6%'
This will work if you are going to have number with more than one digit and there are no spaces between commas.
select * from tablename where prophrases like '%,6,%' or prophrases like '6,%' or prophrases like '%,6'
You need to use a like statement
SELECT * FROM TABLENAME WHERE prophrases LIKE '%6%'
However if you have multiple digit numbers that could cause some unexpected results, so you might have to amend it like
SELECT * FROM TABLENAME
WHERE prophrases LIKE '%,6,%'
OR prophrases LIKE '6,%'
OR prophrases LIKE '%,6'
The first part matches 6 in between commas, the second one matches a 6 at the beginning followed by a comma, the third matches a comma followed by a six a the end.
Storing data like that is not the best way of doing it. You are probably better off having another table that has a one-to-many relationship.
My table contains some columns with ;-separated numbers like this :
1;2;43;22;20;12
and so on. It's also possible there's only 1 number in this column like 110, or 2 numbers like this 110;143
I want to select the rows that contain a certain number in this column. The number is in a variable $search_var.
Let's say I need to search for the number 1 in my column. If I use a select with like statement like so :
"SELECT * FROM Table WHERE ids LIKE '%".$search_var."%'"
I get all results containing '1' and not only '1', so I also get 11, 14, 110, 1999 etc.
I think I need to use some sort of regex-statement but I'm lost here... Who can help ?
You might not need regex for this
Set #YourNUmber := 110;
SELECT *
FROM Table
WHERE ';' + ids + ';' LIKE '%;'+ #yourNumber + ';%'
This guarantees there are always ; surrounding all the numbers.
This is formatted for SQL Server. The variable syntax and wildcards might be different if you are using something else.
EDIT:
Thanks #FĂ©lixGagnon-Grenier for the suggestions. I think either of these two will work. See here for a SQL Fiddle example
SELECT *
FROM T
WHERE concat(';',ids,';') LIKE concat('%;', #YourNumber , ';%');
SELECT *
FROM T
WHERE LOCATE(concat(';', #YourNumber , ';'),concat(';',ids,';'))>0
Try this solution if you're using SQL Server. This searches for the number where adjcent characters are not numbers:
SELECT * FROM Table WHERE ids LIKE '%[^0-9]".$search_var."[^0-9]%'
I have a column in a table that contains data like:
column: col_name
row 1: 3451,3547,234,456,6487,2341,7856,546,76
row 2: 4746,234,6757,234,7657,6346,563
row 3: 546,7467,4562,456,234
row 4: 563,3246,5641,4637,234,7854,567,577
I would like to be able to select a record based on whether it has a certain number in it.
E.g. as 234 appears in all rows, it selects them all; 563 appears in 2 and so on.
This does not work:
select col_name from table_name where col_name like '%234%';
which I think may be because I'm confusing how I should be selecting columns and rows, but I'm at a blank for what else could work, help appreciated!
Edit: I think I'm confusing myself and some of the repliers (sorry, folks). I'm not trying to find the column name that matches my query, I'm trying to find data in the column that matches my query, aka data in the rows. Some of the answers seem geared towards an unknown column name, but I know which column I'm using.
This is what I would expect to get if I search for a row with 563 in it:
4746,234,6757,234,7657,6346,563 (row 2)
563,3246,5641,4637,234,7854,567,577 (row 4)
If I search for it using all the values in the query like so:
select col_name from table_name where col_name = '4746,234,6757,234,7657,6346,563';
then it will return the row:
4746,234,6757,234,7657,6346,563 (row 2)
but I only want to be able to search for it using one number, not several, as they represent different things.
You can do this using FIND_IN_SET:
SELECT col_name
FROM table_name
WHERE FIND_IN_SET('563', col_name);
Example: SQLFiddle
Check out the way you have stored your data in the database,accordingly select the pattern which includes comma n spaces as well.
your query is otherwise correct !!
try this
SELECT col_name
FROM table_name
WHERE concat(',',col_name,',') like '%,234,%';
select
col_name
from table_name
WHERE col_name LIKE '234,%'
OR col_name LIKE '%,234'
OR col_name LIKE '%,234,%'
OR col_name = '234'
Dont worry about rows. It will search through the rows you only have to provide the column name.
try this one
SELECT col_name FROM table_name WHERE ','+col_name+',' like
'%,234,%';