Compare MySQL dates? - mysql

I have 2 columns, start and end.
I need to filter the results to ensure that today's date is between start and end. The date is stored in MM/DD/YYYY syntax. e.g. 02/05/2011
Can anyone show me how this is done please?
"SELECT * FROM albums WHERE active=1 AND ..."
Thanks.

select
*
from
albums
where
active=1
and STR_TO_DATE(begin_date,'%m/%d/%Y') <= CURDATE()
and CURDATE() <= STR_TO_DATE(end_date,'%m/%d/%Y')
But dates shouldn't be stored as varchar. They should be stored as dates.

Try:
SELECT *
FROM albums
WHERE active=1
AND (Comare_Value) between Start and End

SELECT
*
FROM
albums
WHERE
active=1
AND CURRENT_DATE BETWEEN begin_date AND end_date;

SELECT *
FROM albums
WHERE active=1
AND start <= NOW()
AND end >= NOW();

Related

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)

How to convert a mysql datetime column to just date?

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

MYSQL search between two date

I have 2 dates in database like this:
from_dateTime = 2013-06-12
to_dateTime = 2013-07-10
I want to search all records between from_dateTime and to_dateTime. I tried to use following code in mysql:
SELECT *
FROM users i
WHERE i.from_dateTime >= '2013-06-12' AND i.to_dateTime <= '2013-07-10'
But it doesn't work as I expected. Why am I getting empty result set?
UPDATE:
from_dateTime = 2013-06-11
to_dateTime = 2013-06-12
SQL:
SELECT * FROM users i WHERE NOW() between i.from_dateTime AND i.to_dateTime
NOW() is 2013-06-12 08:17:13 and command cant find my record too.
The problem is that you are comparing dates and datetimes. Dates start at midnight. Any time component is larger than the corresponding date. Try this:
SELECT *
FROM users u
WHERE date(NOW()) between u.from_dateTime AND u.to_dateTime
SELECT *
FROM users i
WHERE cast(NOW() as date)
between i.from_dateTime AND i.to_dateTime
SQL FIDDLE
Use between
SELECT * FROM users u where date(NOW()) between u.from_dateTime and u.to_dateTime
select from table_name where datetime_column >= '2013-06-11' and datetime_column <= '2013-06-12'

"Select" Orders from todays date

I have a table within mysql with orders stored in it. I have figured out how to pull everything I need with my SELECT statment. However, I was wondering, does anyone know how to grab orders only from today or even for the week using mysql? The table does contain an "order_date" field which is populated.
You can use this:
... WHERE order_date = CURDATE(); // today
... WHERE order_date BETWEEN CURDATE() - INTERVAL 7 DAY AND CURDATE(); // in the week
Generally speaking (not knowing your schema):
SELECT * FROM orders WHERE order_date >= $start_date AND order_date <= $end_date
You need to define $start_date and $end_date
SELECT * FROM `orders` WHERE DATE(`order_date`) = DATE(NOW())
If the order_date is in datetime
EDIT
For a week
SELECT * FROM `orders` WHERE DATE(`order_date`) >= DATE_SUB(DATE(NOW()),INTERVAL 7 DAY)