SQL Vertica Timestamp - sql

I have three values viz. a date value, an integer representing hour and an integer representing minute value.
I concatenated the three to form a timestamp like string:
CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(CONCAT( CAST(<DATE_VALUE> AS CHAR(10)) , 'T') , CAST(<hour_value> AS CHAR(2))) , ':' ) , CAST(<minutes_value> AS CHAR(2))) , ':' ) , '00' )
Now how do I convert the above created timestamp field another timezone (i.e to EST) ?
Thanks.

You want to use AT TIME ZONE 'EST' but you will need to convert what you have to a timestamp first.
Also, you could just do something like: (VIZ + HR/24 + MIN/24/60) AT TIME ZONE 'EST' instead of all the string contortions. Or you could use INTERVAL like: ( VIZ + (HR::INTERVAL HOUR) + (MIN::INTERVAL MINUTE) ) AT TIME ZONE 'EST'
Example:
=> select ( VIZ + (HR::INTERVAL HOUR) + (MIN::INTERVAL MINUTE) ) AT TIME ZONE 'EST' AS MY_TS
-> FROM (select trunc(now()) AS VIZ, 5 AS HR, 45 AS MIN) x;
MY_TS
------------------------
2015-09-03 05:45:00-05
(1 row)

Related

teradata converting timestamp to time

I'm struggling with something that seems very obvious on first sight and most probably I'm overlooking something stupid but anyway.
I need to calculate the difference between timestamp fields and convert the result (which is as I assume a timestamp ) into the number of days and the elapsed time.
I can't seem to get the cast(xx to time) wright
I made a small example
SELECT
Cast(Cast( c_date AS CHAR(10)) || ' ' || Cast( c_time AS CHAR(10)) AS TIMESTAMP(6)) AS starttime ,
Cast(Cast( e_date AS CHAR(10)) || ' ' || Cast( e_time AS CHAR(10)) AS TIMESTAMP(6)) AS endtm,
(endtm - starttime) DAY(4) TO SECOND AS difftime
,Extract(DAY From difftime) --> gives the days
,Cast(difftime AS TIME)
,Extract (HOUR From difftime)
FROM (
SELECT Cast(Current_Timestamp AS DATE) c_date,
Cast(Current_Timestamp(0) AS TIME(0)) c_time,
Cast(Current_Timestamp + Random(1,10) * INTERVAL '1' DAY AS DATE) e_date,
Cast(Current_Timestamp(0) + Random(1,24) * INTERVAL '1' HOUR + Random(1,60) * INTERVAL '1' MINUTE AS TIME(0)) e_time
) t
,Cast(difftime AS TIME) gives me the trouble
the extract day and hour works => the difftime is really a timestamp (is it ? and if not what kind of field is it then ? ).
some advise would be nice :-)

SQL function to get the sum of datetime column values for each ID

I have a column ID which has values 10,20,10,10,60,60 and so on. I have another column which is a diff of (modified_date - created_date) as TS which has datetime values like 6 5:28:13.0 (6 is the days), 1 1:34:54.0 and so on for each ID. How can I get the total days and time for each ID value? For example: how can I get the total value of TS for ID = 10?
From the image: for each value of eowstage_nbr column, how can I get total days and time in column TS?
A regular sum() aggregation over the ID column combined with some data type conversions should do it...
Sample data
create table data
(
id int,
created datetime,
updated datetime
);
insert into data (id, created, updated) values
(10, '2020-01-01 00:00:00', '2020-01-01 05:00:10'),
(10, '2020-01-02 00:00:00', '2020-01-03 11:00:03'),
(20, '2020-02-05 00:00:00', '2020-02-05 01:02:03'),
(20, '2020-02-15 00:10:00', '2020-02-17 00:00:10');
Solution
Describing the steps in plain English would sound like:
Take the difference between the created and updated timestamp, expressed in seconds;
add them up the seconds for each id;
convert the seconds to a suitable date format.
I took care of step 3 by adding the seconds up to a reference date 1900-01-01.
Query version:
select d.id,
dateadd(second, sum(datediff(second, d.created, d.updated)), '1900-01-01 00:00:00') as diff_sum
from data d
group by d.id;
Result
The values are not formatted like in your screenshot, but that should boil down to data type selection, datetime formatting and localization.
id diff_sum
-- -----------------------
10 1900-01-02 16:00:13.000
20 1900-01-03 00:52:13.000
Fiddle
Since you did not give any sample data and looking at your immage I assume you have data in TS column as per your image. (I have used dummy data to replicate your TS column). So the steps would be to separate day from time in your TS column, then converting that time into seconds, and doing summation of those seconds and days seperately, and finally combining the days and seconds converted to proper d hh:mm:ss format.
select ID,
cast((sum(noofdays) + SUM(DATEDIFF(S, '00:00:00', noofhrs))/3600/24) as varchar(5)) + ' ' +
right('00'+CAST(SUM(DATEDIFF(S, '00:00:00', noofhrs))/3600%24 AS VARCHAR(2)),2) + ':'
+ right('00'+CAST(SUM(DATEDIFF(S, '00:00:00', noofhrs))%3600/60 AS VARCHAR(2)),2) + ':'
+ right('00'+CAST(((SUM(DATEDIFF(S, '00:00:00', noofhrs))%3600)%60) AS VARCHAR(2)),2)
from
(
select ID, case left(TS,charindex(' ',TS)) when '' then 0 else left(TS,charindex(' ',TS)) end as noofdays,
cast(right(TS,len(TS)-charindex(' ',TS)) as datetime) as noofhrs from
(
select 1 ID,'6 5:28:13.0' TS union
select 1,'1 8:28:13.0' a union
select 1,'2 12:28:13.0' a union
select 2,'5:28:13.0' a union
select 2,'6 5:28:13.0' a
)
tablename
) A
group by ID

Is there a way to create a date or timestamp output column from a varchar

I have a data set with the following columns
time date
1310 2020-06-01
1425 2020-06-22
1640 2020-06-29
My desired final output is a column in datetime format that looks as such
datetime
2020-06-01 13:10:00.000
2020-06-22 14:25:00.000
2020-06-29 16:40:00.000
I have used the following command thus far to format the output the way I would like
CONCAT(date_string, ' ', substring(barstarttime, 1, 2), ':', substring(barstarttime, 3, 2), ':00.000'))
However, I have not had success in changing this to a datetime or timestamp.
Is there a function that can help me do so?
Thanks
Get the hours and minutes from the time string with SUBSTR. Then add the according intervals to the date.
I don't know whether Presto allows functions for the interval values. Please try:
select
date +
interval substr(time, 1, 2) hour +
interval substr(time, 3, 2) minute
from mytable;
If this doesn't work, try this instead:
select
date +
((interval '1' hour) * cast(substr(time, 1, 2) as integer)) +
((interval '1' minute) * cast(substr(time, 3, 2) as integer))
from mytable;
As mentioned, I don't know Presto. You may even have to cast the date to a timestamp first:
cast(date as timestamp) +
...
You can use date_parse function.
select date_parse(cast(date as varchar) || ' ' || time, '%Y-%m-%d %H%i') from
(values ('1300', date '2020-06-01')) as t(time, date)
You can use below in order to convert to timestamp
select *,
cast(date_col as timestamp) as ts
from T

Convert Oracle Date into RFC 3339 Format

I have a date field in oracle which returns
17-APR-19 12:00:00 AM
I also have a time column (VARCHAR) which returns HHMM in Military
1810
I'd like to combine these two fields to create a timestamp that is formatted to RFC 3339 standards. Preferable like this.
2019-04-17T18:10:00Z
I can convert a timestamp into the correct time using this:
SELECT
TO_CHAR(
SYSTIMESTAMP AT TIME ZONE 'UTC',
'yyyy-mm-dd"T"hh24:mi:ss"Z"'
)
FROM dual;
Is there a way to convert my date and time field into this timestamp format? The time on the date field is incorrect and needs to be replaced by the time field.
You can TRUNCate your date back to midnight and then use NUMTODSINTERVAL to add hours and minutes to it to get the correct time component:
Oracle Setup:
CREATE TABLE your_table ( your_date_column, your_time_column ) AS
SELECT DATE '2019-04-17', '1810' FROM DUAL
Query:
SELECT TO_CHAR(
TRUNC( your_date_column )
+ NUMTODSINTERVAL( SUBSTR( your_time_column, 1, 2 ), 'HOUR' )
+ NUMTODSINTERVAL( SUBSTR( your_time_column, 3, 2 ), 'MINUTE' ),
'YYYY-MM-DD"T"HH24:MI:SS"Z"'
) AS combined_date_time
FROM your_table
Output:
| COMBINED_DATE_TIME |
| :------------------- |
| 2019-04-17T18:10:00Z |
db<>fiddle here
If you want the value as a TIMESTAMP WITH TIME ZONE then:
SELECT CAST(
TRUNC( your_date_column )
+ NUMTODSINTERVAL( SUBSTR( your_time_column, 1, 2 ), 'HOUR' )
+ NUMTODSINTERVAL( SUBSTR( your_time_column, 3, 2 ), 'MINUTE' )
AS TIMESTAMP
) AT TIME ZONE 'UTC' AS combined_date_time
FROM your_table
Just do a bit of string concatenation
to_char( your_date, 'yyyy-mm-dd' ) ||
'T' ||
substr( your_time, 1, 2 ) ||
':' ||
substr( your_time, 3, 2 ) ||
':00Z'
assuming that your_time is always 4 characters long (i.e. 2 AM is represented as the string '0200' rather than '200'). This also assumes that the seconds will always be '00'.
You can achieve this by converting your_number into minutes and add it to your date, then cast it to timestamp as following:
SELECT CAST(
your_date +
(FLOOR(YOUR_TIME/100)*60 + MOD(YOUR_TIME,100)) / 1440
AS TIMESTAMP
) AT TIME ZONE 'UTC' AS YOUR_TIME_STAMP
FROM your_table;
Cheers!!

creating timestamp from columns in postgres

i have 2 rows in a column, one feeding me the julian date (number of days since Jan 1, 1970, with that day being 1), and the second column is the number of minutes past midnight of the current day (why it was done this way, i have no idea).
i would like to get my sql query to create a timestamp out of these two columns.
if i had access to the data ahead of time, i could do something like SELECT timestamp '1970-01-01 00:00:00' + INTERVAL 'X DAYS' + INTERVAL 'Y MINUTES' AS my_time FROM mytable, but since X and Y are actual members of the table i'm querying, it's not that simple.
anyone have any suggestions?
essentially, i'm looking for the equivalent [legal] solution:
SELECT timestamp '1970-01-01 00:00:00' + INTERVAL 'my_col1 DAYS' + INTERVAL 'my_col2 MINUTES' AS my_time FROm mytable
i can get my server-side code to do it for me, but i would love it if i could build it into my query.
You can construct the interval strings and then cast them to the interval type:
SELECT
timestamp '1970-01-01 00:00:00' +
cast(my_col1 || ' DAYS' AS interval) +
cast(my_col2 || ' MINUTES' AS interval) my_time FROM mytable
Without resorting to string substitution or concatenation:
SELECT
timestamp '1970-01-01 00:00:00' +
my_col1 * interval '1 DAY' +
my_col2 * interval '1 MINUTE' FROM mytable