MySQL query value where current date - mysql

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)

Related

how to filter a select statement in mysql for a particular time period without using now()

I have tried to filter records but with the use of now function as given below
select * from table where date>= DATE_SUB( NOW( ) ,INTERVAL 90 DAY )
What I need is a select statement that can filter its records for a week or month from the current date but without using NOW() function
if you are using java you could make use of the following code
String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
or you could use curdate() of mysql
Since I found it hard to understand the question I provide the following possibilities:
Try for all dates in a week from now:
SELECT * FROM table
WHERE date BETWEEN CURDATE() AND DATE_ADD(CURDATE() ,INTERVAL 1 WEEK)
and for all dates in a month from now:
SELECT * FROM table
WHERE date BETWEEN CURDATE() AND DATE_ADD(CURDATE() ,INTERVAL 1 MONTH)
If you are looking for all dates of the current month use
SELECT * FROM table
WHERE MONTH(date)=MONTH(CURDATE()) AND YEAR(date)=YEAR(CURDATE())
or for all dates of the current week use
SELECT * FROM table
WHERE WEEK(date)=WEEK(CURDATE()) AND YEAR(date)=YEAR(CURDATE())

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'

mysql NOW() with defined time

I need to perform a mysql select query where date time field in table is less then todays date with a time of 2am.
How would I structure this
SELECT * FROM tbl WHERE datetimefield < (DATE(NOW()) and time 2am)
Thank you for any assistance.
This worked:
SELECT * FROM tbl WHERE datetimefield < DATE_ADD(CURDATE(), INTERVAL 2 HOUR)
fiddle - in the fiddle I used a hard coded date '2013-05-04', so it would work correctly later.
SELECT * FROM tbl WHERE datetimefield < STR_TO_DATE(concat(curdate(), ' ', '02'),'%Y-%m-%d %H')

Return rows between two dates in MySQL

I would like to rows that have only been entered in the last 1 day.
I have a date column which stores YYYY-MM-DD, and I allow the user to send a date that they want to look at in this format yyymmdd how can I use this data to limit it to the previous day only?
I would imagine it is something to do with the BETWEEN keyword but I cant figure it out.
SELECT * from TABLE_NAME WHERE ROW_DATE BETWEEN '2011-03-20' AND '2011-03-21'
This query:
SELECT *
FROM mytable
WHERE mydate >= STR_TO_DATE('110321', '%y%m%d') - INTERVAL 1 DAY
AND mydate < STR_TO_DATE('110321', '%y%m%d')
will return all records for Mar 20, 2011
From the MySQL manual (here):
SELECT something FROM tbl_name WHERE DATE_SUB(CURDATE(), INTERVAL 1 DAY) <= date_col;
SELECT * FROM YourTable WHERE date_column = DATE_ADD(CURDATE(), INTERVAL -1 DAY)
This returns all rows for today and yesterday.