mysql using variables with regex - mysql

how do i go about using Mysql variables inside... Mysql regex?
for example,
SET #my_var = (
SELECT
year
FROM blah
LIMIT 1);
SELECT
foo,
bar
FROM my_table
WHERE date REGEXP #my_var%
also tried... WHERE date REGEXP '#my_var%'
what i'm trying to do is... WHERE date REGEXP '2015%'

You would use concat:
where col regexp concat(#my_var, '%')
But two things:
Your pattern looks more like a LIKE pattern than a REGEXP pattern.
Do not use LIKE or REGEXP for dates. MySQL has many useful date functions.
So, I think you want:
where year(date) = 2015
or:
where date >= '2015-01-01' and date < '2016-01-01'
The last version is preferred because it can take advantage of an index, if available.

Another way, depending on how organized you want to be...
SET #var = 101;
SET #regex = concat("^",#var,"$");
SELECT * FROM db.table
WHERE col REGEXP #regex;

Related

How to search on a varchar column using a number?

I have a varchar(30) column that looks like this:
953-41
975-12
952-13
934-34
All numbers of the column share the structure of: 3 numbers and a dash followed by more numbers.
I want to make a query that works like SELECT * FROM tablename WHERE value = 95341
And get '953-41' only using numbers in the WHERE clause.
I can't change my database and remove the dash, I need to search with a numeric value on rows that mix the numbers I want with a dash in between.
you can try:
MYSQL:
SELECT * FROM tablename WHERE value = INSERT(95341,4,0,'-')
SQL Server:
SELECT * FROM tablename WHERE value = STUFF(95341,4,0,'-')
You can use this:
SELECT *
FROM yourTable
WHERE CAST(REPLACE(colName, '-', '') AS UNSIGNED) = 95341

mysql how to compare strings and return rows between two strings

How can I simply filter a table row by alphabets for example showing only rows with strings starting with e,f and g and exclude other rows.
Something like
return (rows > "d") andalso (rows < "h")
in vb.net
you can use like if you want regular expression. for example, this will give you all rows starting with a:
select * from myTable where myColumn like 'a%';
EDIT:
I see what you mean now, mysql supports > and < for strings as well, so if you do:
where col >= 'e' and col < 'h'
you will essentially get everything starting by e,f,g
You can combine multiple LIKE operator saying
select * from table1
where mycol like 'e%'
or mycol like 'f%'
or mycol like 'g%';
(OR) you can use LEFT() string function saying
select * from table1
where left(mycol,1) in ('e','f','g');

SQL Like crashing on "/"

I have a column with datatype varchar using MySQL database. Suppose the value from a web form that gets saved in this column is : 2/4/2013
My search query goes like:
SELECT * FROM tbl WHERE colValue LIKE %2/4/2013%
But, it is crashing. For any other string am getting correct results. But, is it the forward slash which makes it crash. How can this be fixed ?
Regards !
since you want to select for specific date, why not use =
SELECT * FROM tbl WHERE colValue = '2/4/2013'
but if the data type of the column is DATE or DATETIME, use proper formatting although mysql automatically converts it,
SELECT * FROM tbl WHERE colValue = '2013-02-04'
For Using like operator you could use DATEPART() function...
select * from tbl
where (DATEPART(yy, colValue) = 2013
AND DATEPART(mm, colValue) = 04
AND DATEPART(dd, colValue) = 02)
Like this you can do like in SQL
Use an escape character for the /. The mysql escape character is the \.

MySQL REGEXP for numbers that begin with

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.

How do you select from mysql where last character in a string = x?

I'd like to select rows from the database where the last character in the mov_id column equals to 1 and 2.
How would the query look like?
SELECT * FROM `myTable` WHERE `mov_id` LIKE '%1' OR `mov_id` LIKE '%2'
the % character is a wildcard which matches anything (like * in many other places)
If mov_id is a numeric value (TINYINT, INT, etc...) then you should use a numeric operator. For instance, use the modulo operator to keep the last digit
SELECT * FROM mytable
WHERE (mov_id MOD 10) IN (1, 2)
If mov_id is a string, you can use LIKE or SUBSTRING(). SUBSTRING() will be slightly faster.
SELECT * FROM mytable
WHERE SUBSTRING(mov_id, -1) IN ('1', '2')
If your table is big or that query is frequently run, you should definitely consider adding a column to your table, in which you would store mov_id's last digit/character, and index that column.
Try this way too:
SELECT field1
FROM table
WHERE RIGHT(field1, 1) = 'x'
it displays the fields that has last a value of x.
SELECT *
FROM Table
WHERE RIGHT(Column_name, 1) IN ('x')
if you want to match two character just replace 1 by 2.
In general:
RIGHT(COLUMN_NAME, NO_OF_CHARACTER_YOU WANT_TO_MATCH_FROM_LAST)
And if you want to match the starting char just use LEFT instead of RIGHT
You could also do something like:
select
your, fields, go, here
from table
where
substring(mov_id, (char_length(move_id) - 1)) = x
SELECT * FROM table WHERE mov_id REGEXP '1$' OR mov_id REGEXP '2$'