TransWikia.com

Getting midnight of today

Database Administrators Asked by Logu on August 25, 2020

I want to get 00:00:00 of a day. Should be done without using CONCAT. What options are there?

We can use CURDATE() function in select query but need to display like 2014-05-05 00:00:00. If NOW() will give like 2014-05-05 12:05:37.

I want add default value 00:00:00 with CURDATE() in query.

4 Answers

With the MySQL functions CURDATE() and TIMESTAMP

SELECT TIMESTAMP(CURDATE());

it is the same result as

SELECT timestamp(current_date());

or even

SELECT timestamp(current_date);

CURRENT_DATE and CURRENT_DATE() are synonyms for CURDATE().

Note:

SQL commands are case insensitive.

Answered by Froggiz on August 25, 2020

If you have a date like 2014-05-05 12:05:37 you can use DATE_FORMAT.

DATE_FORMAT("2014-05-05 12:05:37", "%Y-%m-%d 00:00:00")

It returns 2014-05-05 00:00:00

Answered by harley81 on August 25, 2020

Another solution would be: cast getdate() to a date, and that back to a datetime.

Code (for MS SQL Server, but the idea applies also to mysql):

select cast(cast(GETDATE() as date) as datetime2)

Answered by til_b on August 25, 2020

The documentation surprisigly contains a solution:

SELECT timestamp(current_date);

(which is basically the same as a comment above).

Answered by dezso on August 25, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP