Modify date NOW() - mysql

I'm trying to set a date to another date.
For example. With NOW() I have 2011-07-14 08:35:00
I want to modify this date to 2011-07-12 00:00:00
So I remove 1 day and I set to 00:00 am.
I have already seen this link -> Set time part of datetime variable to 18:00, but it doesn't work for me. Indeed, I compare :
NOW() with DATEADD(hh, 18, DATEADD(dd, DATEDIFF(dd, -2, THE_DATE_IN_MY_BASE), 0)) and I have an SQL syntax error.

now() returns a datetime value, so hh:mm:ss will be part of it. Sounds more like you want a simple date, so use curdate() instead:
CURDATE() - INTERVAL 1 DAY

Related

Create datetime with specific time mySql

I'm looking to create a date time field in a MySQL script that has a specific date and time.
I've tried using
CONCAT(DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 9 DAY) ,'%Y%m%d'), ' 13:00:00')
but it doesn't insert correctly.
How can I achieve this so that it will insert a date time with the time as above?
It inserts the record as 0000-00-00 00:00:00 with the above
mysql accepts datetime values in yyyy-mm-dd hh:mm:ss format. In your formula you do not separate the year, month, day values, hance the result is not in the date format mysql expects the dates in. Change it to:
CONCAT(DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 9 DAY) ,'%Y-%m-%d'), ' 13:00:00')
But I do not really understand why you need to do the formatting, just use CURDATE() function instead of the NOW():
CONCAT(DATE_SUB(CURDATE(), INTERVAL 9 DAY), ' 13:00:00')

mysql yesterday date with specific timestamp

I am in a situation where i need to query data base to fetch records from yesterday with time stamp, for an example today is 25 so clause should be like:
STR_TO_DATE(created_date,'%Y-%m-%d') BETWEEN '2016-04-24 23:00:00' AND CURRENT_TIMESTAMP
I can get the previous date, but can't set it to 23:00:00 time, any idea's please?
I have tried:
SELECT
DATE_ADD(CURDATE(), INTERVAL - 1 DAY)
, CURRENT_TIMESTAMP
, HOUR(CURRENT_TIMESTAMP)
, DATE_ADD(
CURDATE() INTERVAL - ((HOUR(CURRENT_TIMESTAMP) + 1)) HOUR
)
But it gives an error, i tried to subtract the todays HOURS +1 from current timestamp.
Use
curdate() - interval 1 hour
Since curdate() gives you the current date WITHOUT time. Substract an hour from it and you get yesterday 23:00.

Mysql to get the current date with time 23:59:59

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;
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)

MySQL select dependant on time of day

I want to select from a MySQL table and filter depending on the time of day:
if now > 10am select uploaded date where created date is today
if now < 10am select uploaded date where created date is yesterday
I think you must use Cron jobs for this.
Then, your sql will look like that:
WHERE tbl.TimeCreated = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
assuming this format of the field: DateTime(YYYY-MM-DD HH:MM:SS)
This should do what you need.
SELECT
`uploaded_date`
FROM
`table`
WHERE
`created_date` = CASE WHEN CURTIME() > 100000
THEN CURDATE()
ELSE CURDATE() - INTERVAL 1 DAY
END
You can see a similar example in this fiddle which should return a single row of "AM" in the morning, "Noon" at noon, and "PM" in the afternoon (and 2 empty result sets).

SQL between dates including start and end dates

So I have this:
(CURDATE() BETWEEN start_date AND end_date)
Works fine.
But when the CURDATE() is 2011-12-02 and the end_date is 2011-12-02 will it grab the row?
E.g my start_date is 2011-12-01 00:00:00 and my end date is 2011-12-02 23:59:59
So it only works when the date is between but not if it's ON the end_date itself.
Or maybe it should check for the time too, because it still needs to be selected with this query when it's 2011-12-02 15:30:00 for example.
How can I do this?
Well, you could try
CURDATE() BETWEEN start_date AND DATE_ADD(end_date, INTERVAL 1 DAY)
Since both columns are timestamps, you need to make sure times don't trip you up.
To keep the times from tripping you up, cast the timestamps to date.
where current_date between cast(start_date as date)
and cast(end_date as date);
Maybe the answer to this question refers to a bug in an old version of MySql because between is inclusive, which means it will grab rows between the start and end dates inclusive, not just between the start and one day before the end.
Try this:
SELECT CURDATE() BETWEEN CURDATE() AND CURDATE();
The result is 1 (i.e. true).
I believe the original poster problem lies with mixing up proper dates (DATE) and dates with time (DATETIME or TIMESTAMP).
Try this:
SELECT NOW() BETWEEN CURDATE() AND CURDATE();
SELECT NOW() BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 DAY);
The result is 0 for the first select and 1 for the second. What happened is a DATE is equivalent to a DATETIME with zero time so unless NOW() is called exactly at midnight it will be greater than CURDATE() and fall outside of the BETWEEN statement.
To prevent this test only the DATE part of a DATETIME using the DATE() function:
SELECT DATE(NOW()) BETWEEN CURDATE() AND CURDATE();
Use start_date <= CURDATE() AND end_date > CURDATE()
It will work ... BETWEEN works inclusive of the boundary values. That is,
(CURDATE() BETWEEN start_date AND end_date)
including start_date,end_date and any day falling between
CURDATE() BETWEEN start_date AND ADDDATE(CURDATE(), INTERVAL 1 DAY);
cast (end_date - Start_date as double precision) * 86400