How to convert a mysql datetime column to just date? - mysql

Here is an explanation of how my table is set up (in sqlfiddle)
http://sqlfiddle.com/#!2/86a54d/2
Basically, I have two columns. One is a varchar and contains a name. The other is a datetime and contains a certain date with a time.
I want to select rows where the date in the datetime matches today's date. I have tried things like...
select * from alerts_to_send where date_to_send = now()
select * from alerts_to_send where date_to_send = curdate()
I'm not exactly where to go from here... Any ideas would be greatly appreciated :)
Just to re-clarify, I want to select the rows where today's date matches the date part of my datetime type column.

try this
select * from alerts_to_send
where DATE_FORMAT(date_to_send,'%m-%d-%Y') = DATE_FORMAT(now(),'%m-%d-%Y')
fiddle

You can use the date_format function for mysql
select * from alerts_to_send where date_format(date_to_send, "%m/%d/%Y") = date_format(now(), "%m/%d/%Y")
Here is reference to date_format: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

An alternative to date_format function you can convert the datetime to date:
select * from alerts_to_send where cast(date_to_send as date) = curdate()

It could be even shorter:
select * from alerts_to_send where date(date_to_send) = date(now())
But you will have problem with using index on it so better will be:
select * from alerts_to_send where date_to_send >= date(now()) and date_to_send < date(now()) + INTERVAL 1 DAY

Related

How to check if datetime equal tomorrow in MySQL

I want to retrieve mysql table data if the data created_at Datetime column equal to tomorrow date, for example:
SELECT * FROM sales_order where created_at = tomorrow_date;
You can use the following solution, using DATEDIFF and DATE_ADD:
SELECT *
FROM sales_order
WHERE DATEDIFF(created_at, DATE_ADD(CURDATE(), INTERVAL 1 DAY)) = 0;
or a simpler solution only using DATEDIFF:
SELECT *
FROM sales_order
WHERE DATEDIFF(created_at, CURDATE()) = 1
DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation. - from MySQL docs.
SELECT * FROM sales_order where created_at = CURDATE() + 1;

MySQL Performance DATE_FORMAT() vs YEAR() AND MONTH()

Which is better for performance when looking for timestamps in current month and year?
SELECT *
FROM mytable
WHERE YEAR(CURRENT_TIMESTAMP) = YEAR(mytable.timestamp)
AND MONTH(CURRENT_TIMESTAMP) = MONTH(mytable.timestamp)
OR
SELECT *
FROM mytable
WHERE DATE_FORMAT(CURRENT_TIMESTAMP,'%m-%Y') = DATE_FORMAT(mytable.timestamp,'%m-%Y')
For better perfomance and able to use index in mytable.timestamp, truncate the current date to month.
SELECT DATE_FORMAT(CURRENT_TIMESTAMP, '%Y-%m-01')
This create a constant value and you can index search for it.
And then you can get all the record from this month
SELECT *
FROM mytable
WHERE mytable.timestamp >= DATE_FORMAT(CURRENT_TIMESTAMP, '%Y-%m-01')

How to select year in sql statement

My code:
$results = $GLOBALS['wpdb']->get_results( 'SELECT * FROM myTable WHERE date = 2014 ORDER BY id DESC', object );
The problem is date is stored in this format: 2014-01-01
So how do I select just the year ( I don't care about month and day for the time being ).
Thanks
Use the year() function:
WHERE year(date) = 2014
or use explicit comparisons:
WHERE (date >= '2014-01-01' and date < '2015-01-01')
The latter is better because it can make use of an index on the date column.
Try this Query :
SELECT * FROM myTable WHERE year(`date`)='2014' ORDER BY id DESC
Try this:
SELECT * FROM myTable WHERE date >= '2014-01-01 00:00:00' ORDER BY id DESC
To select all rows where the year of a date column (called date_col) is equal to 2014 use the year function:
SELECT * FROM `tbl` WHERE Year(`date_col`) = '2014';
You can select year for get posts with query_posts(); parameter is year. Example: query_posts("year=2014"); This is not full question for you, only alternative..

MySql, How to display data that was entered in the last 3 hours?

I would like to output recent data that was entered to the DB in the last Three hours, for that I have done this :
SELECT * FROM `tableName` WHERE DATE <= TIMEDIFF ( 'SYSDATE()', '03:00:00' )
But it does NOT worked for me, any ideas on how I can do it ?
You can do this :)
SELECT *
FROM tableName
where Date >= DATE_SUB(NOW(),INTERVAL 3 HOUR);
use timestampdiff on your date column.
SELECT * FROM tableName
WHERE timestampdiff( HOUR, date, now() ) <= 3
Provided your column date must be of type datetype or timestamp

MySQL query value where current date

What is the proper way to query all data on current date? a function in mysql that will get the current date in 12:01 am and current date 11:59 pm
select * from tb_data where date between currentdate_starts and currentdate_ends
Without using DATE(column) = CURDATE()
SELECT * FROM tb_data WHERE date between concat(curdate(),' ','00:00:00') AND concat(curdate(),' ','23:59:59')
more info
Try using CURDATE()
SELECT field FROM table WHERE DATE(column) = CURDATE()
select * from tb_data where DATE(date) = CURDATE()
Documentation: CURDATE
CURDATE() returns the current date.
SELECT * from FROM tb_data WHERE DATE(column) = CURDATE()
Use CURRENT_DATE() or CURDATE() function.
Try this:
SELECT * FROM tb_data WHERE DATE(dateCol) = CURRENT_DATE();
OR
SELECT * FROM tb_data WHERE DATE(dateCol) = CURDATE()
well since there are so many answers. Try this one too and see if it's faster
SELECT * FROM tb_data WHERE date BETWEEN CURDATE() AND (CURDATE()+INTERVAL 1 DAY-INTERVAL 1 SECOND)