How to subtract date in SQL? - mysql

I have date-time field in my database like that 2023-01-18 and I want to subtract this field from the current date.

Use TIMESTAMPDIFF to calculate the difference:
TIMESTAMPDIFF(MINUTE, localtimestamp, columnname) AS difference
localtimestamp gives you the current date and time (without timezone information.) You can also use current_timestamp if you want the timezone.
https://learnsql.com/cookbook/how-to-calculate-the-difference-between-two-timestamps-in-mysql/

Related

Subtract 5 hours from a datetime value that is type timestamp with timezone

I am querying a table that has a datetime column and the value is in the format time stamp with time zone. I've tried doing a select hour(timestamp,-5) as NTime and different variants of that, but the furthest I get is an error stating the timestamp is not a valid name/type. I'm officially going off the deep end on this....
Basically, I just need the new alias column values to be a time stamp that is 5 hours behind the timestamp that is in the original table. Thank you all in advance!!
MariaDB / MySQL doesn't have a "timestamp with timezone" data type.
DATETIMEs are simple wall time, and TIMESTAMPs are UNIX time_t timestamps (the number of seconds since 1970-01-01T00:00:00UTC).
You can convert DATETIME values from one time zone to another by with tz_convert().
SELECT tz_convert('2022-04-08 21:53', 'America/Chicago', 'UTC')
for example.
Or, just to do date arithmetic you can do stuff like this.
SELECT '2022-04-08 21:53' - INTERVAL 5 HOUR

generate timezone specific date and time

My mysql query is as follows:
select * from orders
where orders has created_at field.
Now what i want is i want to convert created_at to 7 am of next day of created_at date . and the time created should be according to local timezone.
Is there a way to achieve it?
Note:: and orders table also has field timezone which has values like Australia/Sydney , Asia/Kolkata etc.
You need to go through the CONVERT_TZ function
// syntax
CONVERT_TZ(your_timestamp_column_name, 'db_timezone', 'your_desired_timezone_name')
// Example
SELECT CONVERT_TZ(`created_at`, 'UTC', `timezone`) as `my_date` FROM orders
Now when you have a date in your desired timezone, you can add hour and time that comes from a difference of next day 7AM and .my_date

day difference between today's date and a specified date in SQL

How to find the day difference between today's date and a specified date in SQL. The specified date column(P.SubscrpEndDate__c) is in Date time format(12/1/2014 12:00 Am). I have used the below query but it do not works
DATEDIFF(day,GETDATE(), P.SubscrpEndDate__c) AS 'SubscriptionDueDate'
In MySQL, DATEDIFF function, takes only two arguments: end date and start date:
DATEDIFF(NOW(), P.SubscrpEndDate__c) AS 'SubscriptionDueDate'
According to the manual:
DATEDIFF(expr1, expr2) returns expr1 − expr2 expressed as a value in days from
one date to the other.
Also, to get the current date and time you have to use NOW() instead of GETDATE.
It should work in SQL Server. I assume date column(SubscrpEndDate__c) in your table would contain lower values than current date, so simply you can use query below. I've just swapped second & third parameters to get positive difference in days. You can also use ABS() function to ignore negative difference.
SELECT DATEDIFF(day,P.SubscrpEndDate__c,GETDATE() ) AS 'SubscriptionDueDate'
This would work, kindly check
SELECT DATEDIFF(NOW(),P.SubscrpEndDate__c) AS SubscriptionDueDate FROM xyz_table WHERE id='123456'

T-SQL Updated date field not the same as date format in select statement

In a SQL Server 2008 database I have a Fiscal Year table where the end_date for each fiscal period is set up wrong. The period start_date is the first day of a calendar month at midnight in the format smalldatetime (2015-01-01 00:00:00). The period end_date is supposed to be the last second of the last day of the start_date's month (2015-01-31 23:59:59). The data type for both fields is smalldatetime.
The following gives me the desired date and time that I would like to put in the end_date field:
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,start_date)+1,0))
But it adds on milliseconds to the end of the time: 2015-01-31 23:59:59.000
When I try converting or casting that as smalldatetime to omit the milliseconds it sets the time to midnight of the last day of the start_date's month: 2015-01-31 00:00:00. This also happens if I just update the end_date field with the code in the select statement above.
How can I update the end_date with the correct format and value (2015-01-31 23:59:59)?
smalldatetime does not store seconds, it's always 00. That's why there's an automatic conversion when you subtract one second.
If you want to store seconds, you have to convert the columns to a different data type.
I didn't realize that smalldatetime doesn't store seconds. KekuSemau's answer wasn't quite the right answer, but it did make me realize I wasn't trying to do the right thing, given the datatype of the column I had to update (No, I couldn't change the datatype of the column).
All I had to do was adjust the query to subtract a minute versus a second, which the smalldatetime field will work with:
cast(DATEADD(MINUTE,-1,DATEADD(mm, DATEDIFF(m,0,start_date)+1,0)) as smalldatetime)
So I ended up updating the end_date column with 2015-01-31 23:50:00, as required.

query between dates returns rows from previous months too

I have the following where clause
AND DATE_FORMAT( l.created_on, "%d/%m/%Y" ) BETWEEN '06/02/2013' AND '07/02/2013'
where created_on is a timestamp.
Now for some reason the query returns rows from previous months as well. anyone knows why?
NOTE :
I need the date to be in that specific format
Mysql string date format follows pattern yyyy-mm-dd. Do not convert to dates if you have timestamps, just compare the timestamps.
WHERE l.created_on
BETWEEN UNIX_TIMESTAMP('2012/02/06') AND UNIX_TIMESTAMP('2013/02/07')
if created_on is already a date (datatype),
you can directly query it using,
WHERE created_on BETWEEN '2013-02-06' AND '2013-02-07'
but if date is a string, use STR_TO_DATE
WHERE STR_TO_DATE(l.created_on, '%d-%m-%Y') BETWEEN '2013-02-06' AND '2013-02-07'
UPDATE 1
since you don't want to change the format of your inputted date, then you need to format it using STR_TO_DATE
WHERE l.created_on BETWEEN STR_TO_DATE('06/02/2013','%d/%m/%Y') AND
STR_TO_DATE('07/02/2013','%d/%m/%Y')