My Sql Date comparision including given date range - mysql

I have date stored as updatedOn (values like '2012-11-12 12:38:43')
My queries is:
select * from mytable
where updatedOn >= '11/12/2012' AND updatedOn <= '11/12/2012'
My goal is to get all records inclusive given from & to dates

If you avoid <= and use < instead, you end up being forced to use logic that works regardless of whether or not your data has a time component.
SELECT
*
FROM
myTable
WHERE
updatedOn >= '2012-12-11'
AND updatedOn < '2012-12-11' + INTERVAL '1' DAY

You need to take the DATE() component of your updatedOn column and format your date literals in one of the formats supported by MySQL:
SELECT *
FROM mytable
WHERE DATE(updatedOn) BETWEEN '2012-12-11'
AND '2012-12-11'
Or else, use MySQL's STR_TO_DATE() function to convert your strings to MySQL dates:
SELECT *
FROM mytable
WHERE DATE(updatedOn) BETWEEN STR_TO_DATE('11/12/2012', '%d/%m/%Y')
AND STR_TO_DATE('11/12/2012', '%d/%m/%Y')

You need to try
select * from mytable
where updatedOn >= '11/12/2012' AND updatedOn <= '11/12/2012 23:59:59.999'
As when you write '11/12/2012' it means '11/12/2012 00:00:00', so you also need to add the time to the string
EDIT: as #Dems pointed out I also needed to take milli seconds into account

You should use DATE function -
SELECT
*
FROM
mytable
WHERE
DATE(updatedOn) = '2012-12-11'

Related

Is there a function similar to Oracle's trunc(sysdate) in mysql

In Oracle I can query like:
Select * from Orders where trunc(createdDate) = trunc(sysdate)
and I get all orders created today.
How to do the same in MySql?
So far I can only find like currentdate() = '07-05-2017', I don't want to hard code the date.
Thank you,
Dapid Candra
The date() function returns the date part of any datetime value and curdate() function returns the current date, so no need to truncate its result:
... date(createdDate)=curdate()
I think a bit faster
Select *
FROM Orders
WHERE createdDate >= curdate();
Yes, you can use MySQL's DATE() function (along with NOW() function to get the current date). Here's the documentation.
Extracts the date part of the date or datetime expression expr.
Your query would be:
Select *
FROM Orders
WHERE DATE(createdDate) = DATE(NOW())
Update
As suggested by #Rahul, we can replace DATE(NOW()) with CURDATE() as it returns date without timestamp, e.g.:
Select *
FROM Orders
WHERE DATE(createdDate) = CURDATE()

How can I compare a expiracion date to be more than NOW() in a MSQL query? I'm trying to validate an expiration date, with a MYSQL query

This is my code that should work but... it doesnt:
SELECT * FROM myTable WHERE token = 'token#' AND expiration_date >= now();
the wierd thing is.. when i do "<=" it bring me dates greater than the actual time.
Maybe is something with the now() configuration time or something.
Comparing the expiration date with now() would only work that way if the data type of expiration_date is datetime else just use
SELECT * FROM myTable WHERE token =
'token#' AND expiration_date >= CURTIME();
if you are comparing with TIME.
or
SELECT * FROM myTable WHERE token =
'token#' AND expiration_date >= CURDATE();
if you are comparing with the DATE.
I think it's just best to set expiration_date as datetime and then compare with now()

how to search current date in mysql database that store standard date as ('Y-m-d H:i:s')?

I store standard date ( with this format: ('Y-m-d H:i:s') ) in mysql database, now i want to select records that match this standard date with current date, in other word i want to select the rows where standard_date field demonstrate today's date.
use DATE() to strip off time in the datetime column. CURDATE() returns the current date.
SELECT *
FROM tableName
WHERE DATE(standard_date) = CURDATE()
SQLFiddle Demo (DATE() vs without DATE())
Just use:
select * from mytable where date(standard_date) = curdate();
select * from tablename where date = CURDATE()
CURDATE() returns the current date.
If you use DATE type, use CURDATE() function -
SELECT * FROM table WHERE date_field = CURDATE()
If you use DATETIME type, use CURDATE() function and DATE() function to get date part from datetime value -
SELECT * FROM table WHERE DATE(date_time_field) = CURDATE()

How to compare timestamp dates with date-only parameter in MySQL?

In a SQL statement, how do I compare a date saved as TIMESTAMP with a date in YYYY-MM-DD format?
Ex.: SELECT * FROM table WHERE timestamp = '2012-05-25'
I want this query returns all rows having timestamp in the specified day, but it returns only rows having midnight timestamp.
thanks
You can use the DATE() function to extract the date portion of the timestamp:
SELECT * FROM table
WHERE DATE(timestamp) = '2012-05-25'
Though, if you have an index on the timestamp column, this would be faster because it could utilize an index on the timestamp column if you have one:
SELECT * FROM table
WHERE timestamp BETWEEN '2012-05-25 00:00:00' AND '2012-05-25 23:59:59'
As suggested by some, by using DATE(timestamp) you are applying manipulation to the column and therefore you cannot rely on the index ordering.
However, using BETWEEN would only be reliable if you include the milliseconds. In the example timestamp BETWEEN '2012-05-05 00:00:00' AND '2012-05-05 23:59:59' you exclude records with a timestamp between 2012-05-05 23:59:59.001 and 2012-05-05 23:59:59.999. However, even this method has some problems, because of the datatypes precision. Occasionally 999 milliseconds is rounded up.
The best thing to do is:
SELECT * FROM table
WHERE date>='2012-05-05' AND date<'2012-05-06'
WHERE cast(timestamp as date) = '2012-05-05'
SELECT * FROM table WHERE timestamp >= '2012-05-05 00:00:00'
AND timestamp <= '2012-05-05 23:59:59'
Use a conversion function of MYSQL :
SELECT * FROM table WHERE DATE(timestamp) = '2012-05-05'
This should work
As I was researching this I thought it would be nice to modify the BETWEEN solution to show an example for a particular non-static/string date, but rather a variable date, or today's such as CURRENT_DATE(). This WILL use the index on the log_timestamp column.
SELECT *
FROM some_table
WHERE
log_timestamp
BETWEEN
timestamp(CURRENT_DATE())
AND # Adds 23.9999999 HRS of seconds to the current date
timestamp(DATE_ADD(CURRENT_DATE(), INTERVAL '86399.999999' SECOND_MICROSECOND));
I did the seconds/microseconds to avoid the 12AM case on the next day. However, you could also do `INTERVAL '1 DAY' via comparison operators for a more reader-friendly non-BETWEEN approach:
SELECT *
FROM some_table
WHERE
log_timestamp >= timestamp(CURRENT_DATE()) AND
log_timestamp < timestamp(DATE_ADD(CURRENT_DATE(), INTERVAL 1 DAY));
Both of these approaches will use the index and should perform MUCH faster. Both seem to be equally as fast.
SELECT * FROM table WHERE DATE(timestamp) = '2012-05-25'
It will work but not used index on "timestamp" column if you have any because of DATE function. below query used index and give better performance
SELECT * FROM table WHERE timestamp >= '2012-05-05 00:00:00'
AND timestamp <= '2012-05-05 23:59:59'
OR
SELECT * FROM table
WHERE timestamp >= '2012-05-05' AND timestamp < '2012-05-06'
Try running these to check stats
explain SELECT * FROM table
WHERE DATE(timestamp) = '2012-05-25'
explain SELECT * FROM table WHERE timestamp >= '2012-05-05 00:00:00'
AND timestamp <= '2012-05-05 23:59:59'
In case you are using SQL parameters to run the query then this would be helpful
SELECT * FROM table WHERE timestamp between concat(date(?), ' ', '00:00:00') and concat(date(?), ' ', '23:59:59')
When I read your question, I thought your were on Oracle DB until I saw the tag 'MySQL'. Anyway, for people working with Oracle here is the way:
SELECT *
FROM table
where timestamp = to_timestamp('21.08.2017 09:31:57', 'dd-mm-yyyy hh24:mi:ss');
Use
SELECT * FROM table WHERE DATE(2012-05-05 00:00:00) = '2012-05-05'
Let me leave here it may help someone
For people coming from nodejs and expressjs
getDailyIssueOperations(dateName, date, status) {
const queryText = `
select count(*) as total from issues
where date(${dateName})='${date}' and status='${status}';
`;
},
in case date and column name are variables please find the implementation usefull

MySQL - SELECT WHERE date < X

How do I do this in MySQL?
SELECT * FROM MyTable WHERE MyDateCol < TODAY()
I googled a lot on the subject and didn't really find anything except "look in the MySQL date ref" which does not explain the above easily enough.
I know I can do the following which is kinda indirect:
SELECT * FROM MyTable WHERE MyDateCol BETWEEN (0000-00-00) AND TODAY()
In MySQL, you have the curdate() funciton, that will get you the date of today.
So, something like this should do :
select *
from your_table
where your_date_column < curdate()
Quoting the manual's page of that function :
Returns the current date as a value in
'YYYY-MM-DD' or YYYYMMDD format,
depending on whether the function is
used in a string or numeric context.
And if you want to compare to the current time (and not only day), you can use the now() function :
Returns the current date and time as a
value in 'YYYY-MM-DD HH:MM:SS' or
YYYYMMDDHHMMSS.uuuuuu format
SELECT * FROM MyTable WHERE MyDateCol < CURDATE()
If MySQL supports SYSDATE, you should use that instead of NOW() or CURDATE(), since it's more standard.
SELECT * FROM MyTable WHERE MyDateCol < SYSDATE;
Use NOW() function:
SELECT * FROM MyTable WHERE MyDateCol < NOW()