If I execute
select current_date;
This will give the current date.
What if I have to get the current date with 23:59:59 , So that it will be end of the day.
In SQL Server I used
Select CAST(CONVERT(VARCHAR(10), GETDATE(), 110) + ' 23:59:59' AS
DATETIME;
MySQL NOW() Function The NOW() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH:MM:SS" (string) or as YYYYMMDDHHMMSS. uuuuuu (numeric).
MySQL CURDATE() Function The CURDATE() function returns the current date. Note: The date is returned as "YYYY-MM-DD" (string) or as YYYYMMDD (numeric). Note: This function equals the CURRENT_DATE() function.
The following is the output. The following is the query to format the date to YYYY-MM-DD. mysql> select str_to_date(LoginDate,'%d. %m.
To insert only date value, use curdate() in MySQL. With that, if you want to get the entire datetime, then you can use now() method. Insert both date and time with the help of now().
One method is to add seconds:
select date_add(CURDATE(), interval 24*60*60 - 1 second)
Another method is addtime()
:
select addtime(CURDATE(), '23:59:59')
Adding interval of 1 day - 1 second] would only work if the datetime / timestamp in question has time set to 00:00:00 (that's the case of CURDATE()
and CURRENT_DATE
).
However, if you need to work with datetime / timestamp set to, for example, 2021-08-05 02:00:00
, the aforementioned method would yield 2021-08-06 01:59:59
.
In such case, you may use DATE_FORMAT:
SELECT expire_at, DATE_FORMAT(expire_at, "%Y-%m-%d 23:59:59")
FROM coupons;
(tested on MySQL 8.0.18)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With