how can I subtract time from a date time field to adjust a date time field? - sql

I currently have a datetime field in the following format: "2019-07-07 15:00:00 UTC". However, this date is an hour and 25 minutes ahead of what it should be. How can I subtract 1 hour and 25 minutes from this time in a new time field?
E.g., "I need 2019-07-07 15:00:00 UTC" to become "2019-07-07 13:35:00 UTC"
Language: SQL
Database: Pulling using Bigquery, which pulls from Google Cloud
The attached image shows the part of my SELECT statement I am working with. The first line is the date but in the database string form, the second line converts it to a date, and the third is trying to transform the date to subtract an hour and 25 mins.
Thanks in advance for the help!

You can try the below for subtracting Hours
SELECT TIMESTAMP_SUB(TIMESTAMP "2019-07-07 15:00:00 UTC", INTERVAL 1 HOUR)
Change your interval accordingly. In your case you can convert it into minutes and then use the MINUTE INTERVAL as shown below
SELECT TIMESTAMP_SUB(starts_at, INTERVAL 85 MINUTE)

Related

SQL - Creating UTC Timestamp from separate date/time/timezone information

I'm working with BigQuery and have a table that looks like:
YEAR
MONTH
DAY
timezone
local time
2015
6
24
America/Los Angeles
1930
Where local time is given by hhmm. I'm wondering if I can format this information into a timestamp column in SQL that yields time in UTC.
I know I can use `TO_TIMESTAMP` but that would involve concatenating all these columns as strings first. Is there any better way to do this? If I were to concatenate, I'm not sure how I would use timezone and then back out UTC.
You might consider below.
WITH sample_table AS (
SELECT 2015 year, 6 month, 24 day, 'America/Los_Angeles' timezone, 1930 local_time UNION ALL
SELECT 2015 year, 6 month, 24 day, 'America/Los_Angeles' timezone, 2400 local_time
)
SELECT TIMESTAMP_SECONDS(
UNIX_SECONDS(TIMESTAMP(DATE(year, month, day), timezone))
+ DIV(local_time, 100) * 3600 + MOD(local_time, 100) * 60
) utc
FROM sample_table;
Since TIME(24, 0, 0) is not a valid time format, the query converts datetime into unix seconds and get back to UTC with the time calculation in seconds.
Input calculates to invalid time: 24:00:00
Query results

how to get Date difference in postgres with date part

How to get datetime difference in postgres
I am using below syntax
DATE_PART('hour', A_column::timestamp-B_column::timestamp )
I want output like this:
If A_column=2020-05-20 00:00:00 and B_column=2020-05-15 00:00:00 I want to get 72(in hours).
Is there any possibility to skip weekends(Saturday and Sunday) in first one, it means to get the result as 72 hours(exclude weekend hours)
If A_column=2020-08-15 12:00:00 and B_column=2020-08-15 00:00:00 I want to get 12(in hours).
You could write this as:
select extract(epoch from a_column::timestamp - b_column::timestamp) / 60 / 60
from mytable
Rationale: substracting the two timestamps gives you an interval; you can then turn it to a number of seconds, and do arithmetics to convert that to hours.

What does TO_DATE('235959', 'HH24MISS') mean?

I came across a SQL query with below conditional clause
To_Char(CRTE_TMS, 'YYYYmmddHH24MISS') between To_Char (TO_DATE(:endDtTime,'YYYYmmddHH24MISS')-TO_DATE('235959', 'HH24MISS')) and :endDtTime
My high level understanding is that create time stamp should be between some time before end time and end time.
Not sure what does TO_DATE('235959', 'HH24MISS') mean.
If I run the below query on 5th Feb it returns 1st Feb
SELECT TO_DATE('235959', 'HH24MISS') FROM DUAl
Please help me understand what exactly this condition mean.
TO_DATE('235959', 'HH24MISS') creates a DATE value. Note, in Oracle data type DATE always contains date and time part.
If you don't provide any date value then Oracle defaults it to the first day of current months, so TO_DATE('235959', 'HH24MISS') returns "2018-02-01 23:59:59"
I don't think this condition makes sense:
To_Char(CRTE_TMS, 'YYYYmmddHH24MISS')
between To_Char (TO_DATE(:endDtTime,'YYYYmmddHH24MISS')-TO_DATE('235959', 'HH24MISS'))
and :endDtTime
First, you should compare DATE values, not strings.
I assume TO_DATE(:endDtTime,'YYYYmmddHH24MISS')-TO_DATE('235959', 'HH24MISS')) is wrong. I think you mean TO_DATE(:endDtTime,'YYYYmmddHH24MISS') - 1 + (1/24/60/60)
This will subtract 1 day plus 1 Second (1/24/60/60), i.e. subtract 23:59:59.
Another possibility would be TO_DATE(:endDtTime,'YYYYmmddHH24MISS') - INTERVAL '23:59:59' HOUR TO SECOND.
So, your condition could be
WHERE CRTE_TMS between TO_DATE(:endDtTime,'YYYYmmddHH24MISS') - 1 + (1/24/60/60) AND :endDtTime
This could probably be a comment instead of an answer.. Sorry do not have enough reputation.
HH24 is the 24 hour format of the hours.
235959 is 23 hours 59 minutes 59 second.
In a 12 hour format it means 11:59:59 PM.
The thing you are trying to do is converting date format into character and comparing it with other dates by converting them to character format using To_char. I do not suggest that.
The below would give the first of the month
SELECT TO_DATE('235959', 'HH24MISS') FROM DUAl;
I am not able to understand what you are trying to achieve here.
The below syntax gives in the character format which is the difference between two dates. for example 4 days and 10 hours.
To_Char (TO_DATE(:endDtTime,'YYYYmmddHH24MISS')-TO_DATE('235959', 'HH24MISS'))
and then you are trying to do a comparision like date between (4 days and 10 hours) and :endtime. This is incorrect.
You could use the below to convert to date format.
to_date('01012018 23:59:59','MMDDYYYY HH24:MI:SS')
select case when to_date('01012018 23:59:59','MMDDYYYY HH24:MI:SS') between :begindate and :enddate then 1
else null
from dual;

SQL to subtract 30 mins from current time and output time should be military format 153010 only time part

SQL to subtract 30 mins from current time and output time should be military format 153010 only time part
In Oracle you can do this with:
SELECT TO_CHAR(SYSDATE - 1 / 24 / 2, 'HH24MISS') FROM DUAL
Adding 1 to a date would increase by a date, 1/24 by an hour, and 1/24/2 by 30 min.
Please note that SYSDATE will give you the time of the server which would normally by UTC. If you want the local time you have to CURRENT_DATE.

Time Difference in Redshift

how to get exact time Difference between two column
eg:
col1 date is 2014-09-21 02:00:00
col2 date is 2014-09-22 01:00:00
output like
result: 23:00:00
I am getting result like
Hours Minutes Seconds
--------------------
3 3 20
1 2 30
using the following query
SELECT start_time,
end_time,
DATE_PART(H,end_time) - DATE_PART(H,start_time) AS Hours,
DATE_PART(M,end_time) - DATE_PART(M,start_time) AS Minutes,
DATE_PART(S,end_time) - DATE_PART(S,start_time) AS Seconds
FROM user_session
but i need like
Difference
-----------
03:03:20
01:02:30
Use DATEDIFF to get the seconds between the two datetimes:
DATEDIFF(second,'2014-09-23 00:00:00.000','2014-09-23 01:23:45.000')
Then use DATEADD to add the seconds to '1900-01-01 00:00:00':
DATEADD(seconds,5025,'1900-01-01 00:00:00')
Then CAST the result to a TIME data type (note that this limits you to 24 hours max):
CAST('1900-01-01 01:23:45' as TIME)
Then LTRIM the date part of the value off the TIME data (as discovered by Benny). Redshift does not allow use of TIME on actual stored data:
LTRIM('1900-01-01 01:23:45','1900-01-01')
Now, do it in a single step:
SELECT LTRIM(DATEADD(seconds,DATEDIFF(second,'2014-09-23 00:00:00','2014-09-23 01:23:45.000'),'1900-01-01 00:00:00'),'1900-01-01');
:)
SELECT LTRIM(DATEADD(seconds,DATEDIFF(second,'2014-09-23 00:00:00','2014-09-23 01:23:45.000'),'1900-01-01 00:00:00'),'1900-01-01');