mysql statement (SELECT) based on date now - mysql

I would like to ask if my Mysql statement is correct or not.. When I run this under mysql it does not return any error but I cannot retrieve the row for it. Here's my statement:
SELECT * FROM timekeeping WHERE createddate = NOW()
Here's what my table looks like

MySQL compare now() (only date, not time) with a datetime field
Try this:
SELECT * FROM timekeeping WHERE DATE(createddate) = DATE(NOW());

Most likely the createddate = NOW() is an exact time comparison , you are probably only interested in the year, month, day being the the same.
See here for details on how to do what you are trying to do:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

Probably you want to search for today date.
Try this:
SELECT * FROM timekeeping WHERE DATE(createddate) = DATE(NOW());

now() includes the time. Given that your fields contain date AND time values, you'll only ever get a match if the date AND time are exact matches. You need to compare dates only:
... WHERE DATE(createddate) = CUR_DATE()
'2014-05-02' = '2014-05-02'
v.s.
... WHERE createddate = now()
'2014-05-02 22:14:00' = '2014-05-02 01:02:03'

My guess is that this isn't exactly what you want: NOW() function will return the exact timestamp for when the query is run, which means you are asking it for any records created at that exact moment in time.
You may want to try something more like:
SELECT * FROM timekeeping WHERE createddate = YOUR_DATE_CRITERIA

Related

Is there a function similar to Oracle's trunc(sysdate) in mysql

In Oracle I can query like:
Select * from Orders where trunc(createdDate) = trunc(sysdate)
and I get all orders created today.
How to do the same in MySql?
So far I can only find like currentdate() = '07-05-2017', I don't want to hard code the date.
Thank you,
Dapid Candra
The date() function returns the date part of any datetime value and curdate() function returns the current date, so no need to truncate its result:
... date(createdDate)=curdate()
I think a bit faster
Select *
FROM Orders
WHERE createdDate >= curdate();
Yes, you can use MySQL's DATE() function (along with NOW() function to get the current date). Here's the documentation.
Extracts the date part of the date or datetime expression expr.
Your query would be:
Select *
FROM Orders
WHERE DATE(createdDate) = DATE(NOW())
Update
As suggested by #Rahul, we can replace DATE(NOW()) with CURDATE() as it returns date without timestamp, e.g.:
Select *
FROM Orders
WHERE DATE(createdDate) = CURDATE()

Query MYSQL with a date in the format of 10-OCT-15

I am trying to query a MYSQL database to return all records with today's date -
SELECT *
FROM credit_application
created_on = '15-OCT-15';
But it's failing because of the 'OCT' part within the query. How can I resolve this please?
Use mysql DATE_FORMAT function
SELECT *
FROM credit_application
DATE_FORMAT(created_on,'%d-%b-%y') = '15-OCT-15';
Use mysql STR_TO_DATE function
SELECT *
FROM credit_application
created_on = STR_TO_DATE('15-OCT-15', '%d-%b-%y');
Of course, this assumes your created_on field is just a DATE. If it's actually a DATETIME or TIMESTAMP, then you'll need to do a range query instead:
SELECT *
FROM credit_application
created_on >= STR_TO_DATE('15-OCT-15', '%d-%b-%y') AND
created_on < DATE_ADD(STR_TO_DATE('15-OCT-15', '%d-%b-%y'), INTERVAL 1 DAY);
The other suggestions of using DATE_FORMAT would require the function to be applied to every row in the table, preventing use of any index you might have. It would be a non-sargable query.

mysql timestamp compare date string

we have a store procedure, the IN parameter is DATE today. in this procedure, a aql is to compare this today value with a table which has a timestamp column.
for example:
column A
2012-12-01 00:00:00
SQL:
select * from t where A = today.
We run this procedure in phpmyadmin, it run OK. but it's not work in command line.
Why?
Guess you may need to format both dates into a common format.. To be safe you may even add Date() or str_to_Date if required...if you are not sure column A contains a proper date...
Try this please:
SELECT * FROM tablename
WHERE DATE_FORMAT(A, '%d/%m/%Y') = DATE_FORMAT(TODAY, '%d/%m/%Y');
if you meant CURDATE() by today then try this as well,
SELECT * FROM tablename
WHERE DATE_FORMAT(A, '%d/%m/%Y') = DATE_FORMAT(CURDATE(), '%d/%m/%Y');
It's possible that the dates are in different formats and that's causing them to be not equal. You can use datediff(date1, date2) = 0 to fix this.
http://www.w3schools.com/sql/func_datediff_mysql.asp

mysql: get record count between two date-time

I am stuck with a problem in MySQL. I want to get the count of records between two date-time entries.
For example:
I have a column in my table named 'created' having the datetime data type.
I want to count records which were created date-time between "TODAY'S 4:30 AM" and "CURRENT DATE TIME".
I tried some of MySQL's functions but still no luck with it.
Can you please help me with this?
thanks.
May be with:
SELECT count(*) FROM `table`
where
created_at>='2011-03-17 06:42:10' and created_at<='2011-03-17 07:42:50';
or use between:
SELECT count(*) FROM `table`
where
created_at between '2011-03-17 06:42:10' and '2011-03-17 07:42:50';
You can change the datetime as per your need. May be use curdate() or now() to get the desired dates.
select * from yourtable where created < now() and created > '2011-04-25 04:00:00'
select * from yourtable
where created < now()
and created > concat(curdate(),' 4:30:00 AM')
for speed you can do this
WHERE date(created_at) ='2019-10-21'

Use MySQL CURDATE() and CURTIME() to return results up and coming but not passed

Users of my site will be able to create events. They will set a date and a time separately. The event will be saved to a mysql database containing separate date and time columns.
I would like to query the database to return events which are up and coming but not passed. i.e. the event date is today or in the future and if the event date is today check the the time of the event hasn't passed.
I don't know the syntax for this and I cant seem to find it anywhere.
If anyone knows a good way to do this I'd very much appreciate your help.
The simplest way to express this efficiently in SQL and make use of any indexes you have on the date and/or time columns would be a query like this:
SELECT *
FROM your_table
WHERE date_column > current_date()
OR (date_column = current_date() AND time_column > current_time())
Depending on which version of MySQL you are using and how your table is indexed there is a small chance that the optimizer would prefer the query expressed as two SELECTs UNIONed together to avoid the OR:
SELECT *
FROM your_table
WHERE date_column > current_date()
UNION
SELECT *
FROM your_table
WHERE date_column = current_date()
AND time_column > current_time()
select * from table where concat_ws(' ',date_field,time_field) > now()
Why don't you use a datetime field?
in pseudocode
if ((date > today) OR (date == today AND time > now))