Is there a function similar to Oracle's trunc(sysdate) in mysql - 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()

Related

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 statement (SELECT) based on date now

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

how to search current date in mysql database that store standard date as ('Y-m-d H:i:s')?

I store standard date ( with this format: ('Y-m-d H:i:s') ) in mysql database, now i want to select records that match this standard date with current date, in other word i want to select the rows where standard_date field demonstrate today's date.
use DATE() to strip off time in the datetime column. CURDATE() returns the current date.
SELECT *
FROM tableName
WHERE DATE(standard_date) = CURDATE()
SQLFiddle Demo (DATE() vs without DATE())
Just use:
select * from mytable where date(standard_date) = curdate();
select * from tablename where date = CURDATE()
CURDATE() returns the current date.
If you use DATE type, use CURDATE() function -
SELECT * FROM table WHERE date_field = CURDATE()
If you use DATETIME type, use CURDATE() function and DATE() function to get date part from datetime value -
SELECT * FROM table WHERE DATE(date_time_field) = CURDATE()

My Sql Date comparision including given date range

I have date stored as updatedOn (values like '2012-11-12 12:38:43')
My queries is:
select * from mytable
where updatedOn >= '11/12/2012' AND updatedOn <= '11/12/2012'
My goal is to get all records inclusive given from & to dates
If you avoid <= and use < instead, you end up being forced to use logic that works regardless of whether or not your data has a time component.
SELECT
*
FROM
myTable
WHERE
updatedOn >= '2012-12-11'
AND updatedOn < '2012-12-11' + INTERVAL '1' DAY
You need to take the DATE() component of your updatedOn column and format your date literals in one of the formats supported by MySQL:
SELECT *
FROM mytable
WHERE DATE(updatedOn) BETWEEN '2012-12-11'
AND '2012-12-11'
Or else, use MySQL's STR_TO_DATE() function to convert your strings to MySQL dates:
SELECT *
FROM mytable
WHERE DATE(updatedOn) BETWEEN STR_TO_DATE('11/12/2012', '%d/%m/%Y')
AND STR_TO_DATE('11/12/2012', '%d/%m/%Y')
You need to try
select * from mytable
where updatedOn >= '11/12/2012' AND updatedOn <= '11/12/2012 23:59:59.999'
As when you write '11/12/2012' it means '11/12/2012 00:00:00', so you also need to add the time to the string
EDIT: as #Dems pointed out I also needed to take milli seconds into account
You should use DATE function -
SELECT
*
FROM
mytable
WHERE
DATE(updatedOn) = '2012-12-11'

how to test date and datetime with mysql

My table is using a datetime (YYYY-MM-DD HH:MM:SS) and i need to display today's entries.
my code is only :
SELECT *
FROM table
WHERE date = '$date'
ORDER BY score DESC
with
$date = date("Y-m-d");
well, as expected it doesnt work :| you guys have a solution here ?
Following from Pascal Martin, you could extract the date part from the date+time field:
SELECT * FROM table WHERE DATE(date) = '2009-12-19'
Source: MySQL - Date and Time Functions
Be aware however, that this query will not use an index on your date+time field, if you will be having one. (Stack Overflow: How does one create an index on the date part of DATETIME field in MySql)
Your date is "2009-12-19" (or something like that, depending on the day), which is interpreted as "2009-12-19 00:00:00".
In your database, you probably don't have any date that's exactly equal to that one, by the second : your dates are like "2009-12-19 12:15:32".
A solution is to compare like this :
select *
from table
where date >= '2009-12-19'
and date < '2009-12-20'
Which will be interpreted as :
select *
from table
where date >= '2009-12-19 00:00:00'
and date < '2009-12-20 00:00:00'
And, if you don't want to do the math to get the date of the following date, you can use the adddate function :
select *
from table
where date >= '2009-12-19'
and date < adddate('2009-12-19', interval 1 day)
So, in your case, something like this should do the trick :
select *
from table
where date >= '$date'
and date < adddate('$date', interval 1 day)
order by score desc
You probably want to format the data when you select it:
SELECT *, DATE_FORMAT(date, '%Y-%m-%d') AS dateformat FROM table
WHERE dateformat = '$date' ORDER BY score DESC
You are comparing datetime and date expression, that is why its not working. Use Date() method to return the date part from datetime and then do the comparison. WHERE DATE(date) = '$date' should do. You might have to use aliases to handle this name collision.