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'
Related
My current code select all the date that's less than 30days, how can i select date that's exactly 30 days later,many thanks
$result = mysql_query("SELECT * FROM patientvaccinedetail WHERE ( NOW( ) - INTERVAL 1 MONTH)")
You need to use DATEDIFF() function for this, e.g.:
SELECT *
FROM patientvaccinedetail
WHERE DATEDIFF(NOW(), yourdatecolumn) = 30;
You can simply use DATE_ADD(your_field_name, INTERVAL X DAYS) method to add date to the date that exists in your table.
This should work
select
*
from x
where
trunc(something) = trunc(sysdate-30)
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..
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)
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
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();