Comparing date values of CURDATE() with a full timestamp field - mysql

I have a function that is placing timestamp values (YYYY-MM-DD HH:MM:SS) into META_VALUE column of table META.
What I want to do is to compare whether the date portion (YYYY-MM-DD) of the META_VALUE is equal to today (CURDATE()), disregarding the hour, minute and second (HH:MM:SS).
How can I accomplish this?

SELECT * FROM table WHERE <timestamp-field> BETWEEN 'YYYY-MM-DD 00:00:00' AND 'YYYY-MM-DD 23:59:59'
Allways avoid doing calculations on the field if possible: e.g.
SELECT * FROM table WHERE DATE(<timestamp-field>) = 'YYYY-MM-DD'
will calculate DATE() for ALL rows in that table, so you are really talking wasted cycles here

Simply use DATE:
SELECT * FROM table WHERE DATE(timestamp) = '2011-12-29'

Related

query for date range '01-01-2019' and '02-02-2019' is not working

I have a query with date range like between '01-01-2019' and '03-02-2019' and it is working while I change day(02) of date range like '01-01-2019' and '02-02-2019' then Query is not working.
I have a query with date range from '01-01-2019' and To : '03-02-2019' like
SELECT * FROM `customer_info` WHERE `rec_date` between '01-01-2019' and '03-02-2019'
so it is working but when I change the day of date range like from '01-01-2019' and To : '02-02-2019' and query is
SELECT * FROM `customer_info` WHERE `rec_date` between '01-01-2019' and '02-02-2019'
But query is not working.
I want from this query that it should fetch records after changing day of date range of TO : '02-02-2019'.Please tell me solution why it is not fetching records.
With Only Date:
MySQL date format is this : YYYY-MM-DD. In a simple way it can be queried as
select * from customer_info where rec_date between '2019-01-01' and '2019-02-02'
OR
select * from customer_info where rec_date between >= '2019-01-01' and '2019-02-02'
With Date & Time:
Just use the format which MySQL handles dates YYYY-MM-DD HH:MM:SS and have the query as
select * from customer_info where rec_date >= '2019-01-01' and rec_date <= '2019-02-02'
Especially since MySQL now supports sub-second time precision. Using 23:59:59 is one full second short of the end of a day. where rec_date >= '2019-01-01' and rec_date <= '2019-02-02' is the most precise way to define a date range and will work for any level of time precision from date to sub-second units.
OR
Simple as that.
select * from customer_info where rec_date between '2019-01-01 00:00:00' and '2019-02-02 23:59:00'
That rec_date is probably a VARCHAR or CHAR type instead of a DATE type.
Then it should be transformed to a DATE type before getting a range from that.
Otherwise the BETWEEN will compare the strings alphabetically.
And when you then query a date range, use the 'YYYY-MM-DD' format for date constants.
That won't lead to a confusion with the months and days.
(Is '01-04-2019' the 1st of April, or the 4th of January?)
SELECT *
FROM `customer_info`
WHERE STR_TO_DATE(rec_date,'%m-%d-%Y') BETWEEN '2019-01-01' and '2019-03-02'

mysql and date comparison with date function and without

I notice a strange behavior for me when using date() and without:
when I use
SELECT * FROM `mytable` WHERE date(date_add) >= '2017-08-01' and date(date_add) <= '2017-08-31'
I get all dates records within the given date range but if do:
SELECT * FROM `mytable` WHERE date_add >= '2017-08-01' and date_add <= '2017-08-31'
I don't get the rows from the last day 31, why? (the date_add field is datetime type)
EDIT:
How should I code date range correctly? Because what I understand so far is that if I don't use full time like YYYY-MM-DD HH-MM-SS I should always compare with DATE() to avoid missing results from the last day.
That's because date_add is a DATETIME field. If the time for the date 2017-08-31 is something like 08:15:00 or 13:21:00 in your table, your date is "bigger" than just 2017-08-31 00:00:00. Your comparison would just return data for the 2017-08-31 having the time 00:00:00.
Because without the date() transformation your comparison imply the values '2017-08-01 00:00:00' and '2017-08-01 00:00:00'

How to compare a date when the date is stored as a varchar

The dates in my database are stored as varchars instead of date formats due to the way it was first built.
The dates look like this:
e.g. 1/3/2015 and
10/3/2015
I'm trying:
"SELECT COUNT(*) n FROM tracker WHERE TIMESTAMP(STR_TO_DATE(date, '%d/%m/%Y'))<=NOW()"
However, that's not working. It is returning the count of all records, regardless of the date.
How can I count only the records where the date is today or in the past?
You do not need TIMESTAMP():
SELECT COUNT(*) as n
FROM tracker
WHERE STR_TO_DATE(date, '%d/%m/%Y') <= NOW()
You should pay attention to the functions STR_TO_DATE and NOW(), the first return a date, the second is a timestamp.
When you convert STR_TO_DATE(date, '%d/%m/%Y') you will get a date with hours, minutes and seconds as 00:00:00
Using CURRENT_DATE perhaps will match more closely the original requirements
SELECT COUNT(*) as n
FROM tracker
WHERE STR_TO_DATE(date, '%d/%m/%Y') <= CURRENT_DATE
Also I suggest you to rename the column 'date'

How to fetch elements with current date in MYSQL when we have only Timestamp field with DateTime values

I have a table with field Timestamp(DateTime format). I need to get elements that are entered on the current date. Kindly suggest me a way/query to do so. How to fetch current current date out of the timestamp and compare it with current date via query. HELP!!!
You can use the function curdate() function
select * from table_name where date(col_name) = curdate();
Or even better when you have large volume of data and the timestamp column is index and you want index to take into account which will not when you use date() function you can do as
select *
from table_name
where col_name between concat(curdate(),' 00:00:00') and concat(curdate(),' 23:59:59');

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