Compare dates in MySQL - mysql

I want to compare a date from a database that is between 2 given dates.
The column from the database is DATETIME, and I want to compare it only to the date format, not the datetime format.
SELECT * FROM `players` WHERE CONVERT(CHAR(10),us_reg_date,120) >= '2000-07-05' AND CONVERT(CHAR(10),us_reg_date,120) <= '2011-11-10'
I get this error when I execute the SQL above:
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near
'us_reg_date,120) >= '2000-07-05' AND
CONVERT(CHAR(10),us_reg_date,120) <=
'2011-' at line 1
How can this problem be fixed?

You can try below query,
select * from players
where
us_reg_date between '2000-07-05'
and
DATE_ADD('2011-11-10',INTERVAL 1 DAY)

That is SQL Server syntax for converting a date to a string. In MySQL you can use the DATE function to extract the date from a datetime:
SELECT *
FROM players
WHERE DATE(us_reg_date) BETWEEN '2000-07-05' AND '2011-11-10'
But if you want to take advantage of an index on the column us_reg_date you might want to try this instead:
SELECT *
FROM players
WHERE us_reg_date >= '2000-07-05'
AND us_reg_date < '2011-11-10' + interval 1 day

This works:
select date_format(date(starttime),'%Y-%m-%d') from data
where date(starttime) >= date '2012-11-02';
Note the format string %Y-%m-%d and the format of the input date. For example 2012-11-02 instead of 12-11-2.

I got the answer.
Here is the code:
SELECT * FROM table
WHERE STR_TO_DATE(column, '%d/%m/%Y')
BETWEEN STR_TO_DATE('29/01/15', '%d/%m/%Y')
AND STR_TO_DATE('07/10/15', '%d/%m/%Y')

this is what it worked for me:
select * from table
where column
BETWEEN STR_TO_DATE('29/01/15', '%d/%m/%Y')
AND STR_TO_DATE('07/10/15', '%d/%m/%Y')
Please, note that I had to change STR_TO_DATE(column, '%d/%m/%Y') from previous solutions, as it was taking ages to load

Related

Adding timezone offset in the datetime in SQL

Would like to convert timestamp stored in SQL to a specific timezone.
SQL Query to offset created_at time by -33000
select cast(created_at - 33000 as date) from table_name
The query is working fine but it's giving NULL for timestamp 2019-11-14 02:52:31.
Any hints is appreciated.
Try this:
SQL SERVER:
Your query will cause some explicit conversion error. So you have to convert it into proper Datetime, and then try you query.
select cast(CAST('2019-11-14 02:52:31' AS DATETIME) - 33000 as date)
MySQL
You have to use DATEADD function.
SELECT DATE_ADD('2019-11-14 02:52:31', INTERVAL -330 MINUTE);
The output will be 2019-11-13 21:22:31
Hope this will fix your issue.

Query MYSQL with a date in the format of 10-OCT-15

I am trying to query a MYSQL database to return all records with today's date -
SELECT *
FROM credit_application
created_on = '15-OCT-15';
But it's failing because of the 'OCT' part within the query. How can I resolve this please?
Use mysql DATE_FORMAT function
SELECT *
FROM credit_application
DATE_FORMAT(created_on,'%d-%b-%y') = '15-OCT-15';
Use mysql STR_TO_DATE function
SELECT *
FROM credit_application
created_on = STR_TO_DATE('15-OCT-15', '%d-%b-%y');
Of course, this assumes your created_on field is just a DATE. If it's actually a DATETIME or TIMESTAMP, then you'll need to do a range query instead:
SELECT *
FROM credit_application
created_on >= STR_TO_DATE('15-OCT-15', '%d-%b-%y') AND
created_on < DATE_ADD(STR_TO_DATE('15-OCT-15', '%d-%b-%y'), INTERVAL 1 DAY);
The other suggestions of using DATE_FORMAT would require the function to be applied to every row in the table, preventing use of any index you might have. It would be a non-sargable query.

Select date between current date and one week InnoDB

I have table with one column (date of expiration). I want to select all rows where expiration is between today and one next week. I am using MySQL and InnoDB type.
I try something like this:
SELECT name, expiration
FROM exp
WHERE (expiration BETWEEN(CURRENT_DATE, INTERVAL 1 WEEK));
But i have bad syntax. There is error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 0, 30' at line 3
Try this:
SELECT *
FROM exp
WHERE expiration BETWEEN
current_date
AND
current_date + interval 7 day
demo: --> http://www.sqlfiddle.com/#!2/8598a/2
This should be the syntax you want:
SELECT name, expiration FROM table WHERE expiration
BETWEEN
CURRENT_DATE
AND
ADDDATE(CURRENT_DATE, INTERVAL 1 WEEK)
(You need to add the one week to the current date, you cannot just specify the interval standing on its own/as a parameter to BETWEEN)

sql telling how many days have passed(datediff)

I'm working on a project where I want to display data after 3 days have passed.
What I'm having an issue with is getting the current date dynamically in php/sql. I'm aware of how to get the current date in php, but I dont know how to compare that value to the date that I have in the sql database.
You can do that directly in SQL
select * from your_table
where date_column <= curdate() - interval 3 day
You can use an interval select to limit the records to within 3 days if the column you're checking istimestamp, date, or datetime.
select * from tablename where timestamp_column >= NOW() - INTERVAL 3 DAY
You can use DATEDIFF function to check for days.
SELECT *
FROM tablename
WHERE DATEDIFF(CURRENT_DATE(), datecol) >= 3;

How to convert/cast varchar to date?

I have a date column with data type varchar(mm-dd-yyyy) in mySQL 5.1. How do I convert it to DATE?
Here is what I have so far -
SELECT id, date
FROM tableName
WHERE (CAST((SUBSTRING (date FROM 7 FOR 4 )||'/'||SUBSTRING (date FROM 4 FOR 2 )||'/'||SUBSTRING (date FROM 1 FOR 2 )) AS DATE) >= '01/01/2012' )
ORDER BY date DESC;
Getting this
error - #1064 - You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'FROM 7 FOR 4 )
Please help.
You can use MySQL's STR_TO_DATE() function
SELECT id, date
FROM tableName
WHERE STR_TO_DATE(date,'%Y-%m-%d') >= '01/01/2012'
ORDER BY date DESC;
Although I suspect you will have an easier time using Unix Timestamps
SELECT id, date 
FROM tableName 
WHERE UNIX_TIMESTAMP(STR_TO_DATE(date,'%d/%m/%Y')) >= UNIX_TIMESTAMP('01/01/2012') 
ORDER BY date DESC;