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 \.
Related
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
i have in my table places named field. there are space separated values(there are problem to store csv value in one field). now i want to fire query like below. how i can do ??
select * from tablename where variablename in places
i did try this way but it shows syntax error.
select * from tablename where variablename in replace(places,' ',',')
### places ###
bank finance point_of_interest establishment
Use FIND_IN_SET
For comma separated
SELECT *
FROM tablename
WHERE ( FIND_IN_SET( 'bank', variablename ) )
Refer : SQL Fiddle
For space separated
SELECT *
FROM tablename
WHERE ( FIND_IN_SET( 'bank', replace(variablename,' ',',') ) )
Refer : SQL Fiddle
The best solution would be to normalise your data structure and do not have a single field storing multiple values.
You can make a query work without normalisation, but any solutions would be lot less optimal from a performance point of view.
Use patter matching with like operator:
... where fieldname like '% searched_value %'
Use the replace() function and combine it with find_in_set():
... where find_in_set('searched_value',replace(fieldname,' ',','))>0
Hi I think your problem comes from the usage of IN
IN for MySql is used like this
SELECT *
FROM table_name
WHERE column_name IN (bank,finance,point_of_interest, establishment);
In case of you want to select places you need to specify each place into value like
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;
I am trying to find all values that do not start with a number
I have tried this query, but not sure if the REGEXP is correct. This seem to be returning any value the does not contains a number
SELECT * FROM table where address NOT REGEXP '[0-9]'
I think this fixed the issue
SELECT * FROM table where address NOT REGEXP '^[0-9]'
In MySQL if you try to convert a string to a number then it will start from the beginning taking the digits and convert as much as possible.
If a string does not start with a number then the result is 0.
select * from your_table
where address * 1 = 0
SQLFiddle demo
You can use explicit conversion with cast(str_column as unsigned) or implicit conversion by using a mathematical operator like * 1
I am trying to perform the following query:
SELECT * FROM table_name_here WHERE Date LIKE %Jan % 2014%
Now, the table name is different and hidden here, but it just won't go through. It says there is an error in my syntax around %Jan % 2014%
I can get this to work, so I know the connection works: SELECT * FROM table_name_here
So the problem lies with the WHERE and LIKE part.
I also tried to perform this on my hosting sites DB management tool:
SELECT *
FROM `table_name`
WHERE `Date` LIKE '%Jan % 2014%'
and that one works
You have two syntax errors, firstly the word Date is a keyword, so needs to be wrapped and you need quotes around your string, like so:
SELECT * FROM table_name_here WHERE `Date` LIKE "%Jan % 2014%"
Assuming that date is begin stored as a date/datetime column, don't use like on it. The like implicitly converts it to a string, using some local format.
Instead, be explicit:
where month(`date`) = 1 and year(`date`) = 2014
or
where date_format(`date`, '%Y-%m') = '2014-01'
As for your original question, you discovered that quotes are important around string constants. I would recommend using single quotes (as opposed to double quotes), because single quotes are the ANSI standard string delimiter.
You need quotes around your LIKE match:
SELECT * FROM table_name_here WHERE Date LIKE "%Jan % 2014%"
If you are using the DATE datatype, you can use this
SELECT * FROM table_name_here WHERE 'Date' LIKE '2014-01-%'
this is how mysql store the DATE value: 'YYYY-MM-DD' eg. '2014-01-07.
MySQL Manual