Vertica String Timezone conversion - sql

I have a list of 'HH24:MI:SS' stored as a string and I need to factor in the timezone.
Is there a way to select a.hour at timezone from database with how it's currently stored?
11:30:00
11:00:00
12:00:00
not sure if there's a way or not. I've tried converting to a date or timestamp but no luck since it's stored as a string. I feel like theres a simple way but I'm not seeing it

Do you mean this?
WITH
indata(s) AS (
SELECT '11:30:00'
UNION ALL SELECT '11:00:00'
UNION ALL SELECT '12:00:00'
)
SELECT
s::TIME AS the_time
, s::TIME AT TIMEZONE 'America/New_York' AS the_other_time
, HOUR(s::TIME) AS the_hour
, HOUR(s::TIME AT TIMEZONE 'America/New_York') AS the_other_time
FROM indata;
-- out the_time | the_other_time | the_hour | the_other_time
-- out ----------+----------------+----------+----------------
-- out 11:30:00 | 05:30:00-05 | 11 | 5
-- out 11:00:00 | 05:00:00-05 | 11 | 5
-- out 12:00:00 | 06:00:00-05 | 12 | 6

Related

A Decimal String Style of Datestamp from an Airtable to a Correct and True DateStamp in a Big Query

In Airtable the DateTime Data is correct but if you upload it in the Big Query, it will transform into a Decimal String Style of Numbers. See the table below:
Date
True Date
44493.61666667
2021-10-24T14:48:00
44522.775
2021-11-22T18:36:00
44493.67916666667
2021-10-24T16:18:00
44522.739583333336
2021-11-22T17:45:00
This is the problem I have encountered. Assuming the table name is Airtable. Can you give a suggested query to convert it into the correct and true timestamp?
Actually, I post it also on my blog. Thank you for your suggested answers.
I don't have access to BigQuery -
but I found out that your "Date" value is a division seconds to days (seconds / 86400) since TIMESTAMP '1899-12-30 00:00:00' :
WITH
airtable(date,true_date) AS (
SELECT 44493.61666666667 ,TIMESTAMP '2021-10-24T14:48:00'
UNION ALL SELECT 44522.775 ,TIMESTAMP '2021-11-22T18:36:00'
UNION ALL SELECT 44493.67916666667 ,TIMESTAMP '2021-10-24T16:18:00'
UNION ALL SELECT 44522.739583333336,TIMESTAMP '2021-11-22T17:45:00'
)
SELECT
*
, true_date - "date" AS anchor_ts
, CAST (("date" * 86400) AS INTEGER) AS secs_snc_anchor
FROM airtable;
-- out date | true_date | anchor_ts | secs_snc_anchor
-- out --------------------+---------------------+---------------------+-----------------
-- out 44493.616666666670 | 2021-10-24 14:48:00 | 1899-12-30 00:00:00 | 3844248480
-- out 44522.775000000000 | 2021-11-22 18:36:00 | 1899-12-30 00:00:00 | 3846767760
-- out 44493.679166666670 | 2021-10-24 16:18:00 | 1899-12-30 00:00:00 | 3844253880
-- out 44522.739583333336 | 2021-11-22 17:45:00 | 1899-12-30 00:00:00 | 3846764700
So you could add your "Date" value to TIMESTAMP '1899-12-30 00:00:00' to get your desired value.
In many databases (Oracle, PostgreSQL, Vertica, etc), it would be like in the query below.
In Big Query, it should be:
TIMESTAMP_ADD(TIMESTAMP "1899-12-30 00:00:00", INTERVAL "Date" MINUTE) AS calculated_ts.
And in the second query, I renamed "Date" to dt, as I shy away from using reserved words (a type designator in this case) for database objects.
WITH
airtable(dt,true_date) AS (
SELECT 44493.61666666667 ,TIMESTAMP '2021-10-24T14:48:00'
UNION ALL SELECT 44522.775 ,TIMESTAMP '2021-11-22T18:36:00'
UNION ALL SELECT 44493.67916666667 ,TIMESTAMP '2021-10-24T16:18:00'
UNION ALL SELECT 44522.739583333336,TIMESTAMP '2021-11-22T17:45:00'
)
SELECT
*
, TIMESTAMPADD(ss,CAST ((dt * 86400) AS INTEGER), TIMESTAMP '1899-12-30 00:00:00') AS derived_ts
FROM airtable;
-- out dt | true_date | derived_ts
-- out --------------------+---------------------+---------------------
-- out 44493.616666666670 | 2021-10-24 14:48:00 | 2021-10-24 14:48:00
-- out 44522.775000000000 | 2021-11-22 18:36:00 | 2021-11-22 18:36:00
-- out 44493.679166666670 | 2021-10-24 16:18:00 | 2021-10-24 16:18:00
-- out 44522.739583333336 | 2021-11-22 17:45:00 | 2021-11-22 17:45:00
Can you give a suggested query to convert it into the correct and true timestamp?
Use following expression timestamp_seconds(cast((date - 25569) * 86400 as int))
You can test it with the sample data in your question as in below example
with airtable as (
select 44493.61666666667 date, '2021-10-24T14:48:00' expected_true_date union all
select 44522.775 , '2021-11-22T18:36:00' union all
select 44493.67916666667 , '2021-10-24T16:18:00' union all
select 44522.739583333336, '2021-11-22T17:45:00'
)
select *,
timestamp_seconds(cast((date - 25569) * 86400 as int)) calculated_true_date
from airtable
output

How to convert a number (YYMMDDhhmmss) into timestamp in Oracle's SQL?

I am trying to calculate a difference between systimestamp and a number in Oracle's SQL.
The number format is YYMMDDhhmmss (For example 190903210000). This number is held in a table and is based on 24 hour timezone.
I am trying to calculate a difference that number and system timestamp in seconds.
I have to use a select sentence as below:
SELECT X FROM table
Then I need to calculate a difference (SYSTEMTIMESTAMP - X)
My system timestamp is formatted like 03-SEP-19 06.21.49.817757 PM +00:00
Could you please advise me on the right approach?
To convert a NUMBER to a TIMESTAMP you can use an expression like TO_TIMESTAMP(TO_CHAR(...)). To compute the number of seconds between two timestamps, one solution is to cast both to dates, and substract them : you will get a (decimal) result in days, which can be then converted to seconds.
Consider:
(
CAST(systimestamp AS DATE)
- CAST(TO_TIMESTAMP(TO_CHAR(190903210000), 'YYMMDDhh24miss') AS DATE)
) * 60 * 60 * 24
However, since your numeric representation of the timestamp does not contain fractional seconds (nor timezone), it would probably be simpler to convert directly to a DATE, which would remove the need to CAST it afterwards, hence:
(
CAST(systimestamp AS DATE)
- TO_DATE(TO_CHAR(190903210000), 'YYMMDDhh24miss')
) * 60 * 60 * 24
Demo on DB Fiddle:
SELECT
systimestamp,
190903210000 num,
TO_TIMESTAMP(TO_CHAR(190903210000), 'YYMMDDhh24miss') num_as_timestamp,
(
CAST(systimestamp AS DATE) -
CAST(TO_TIMESTAMP(TO_CHAR(190903210000), 'YYMMDDhh24miss') AS DATE)
) * 60 * 60 * 24 diff
FROM DUAL;
SYSTIMESTAMP | NUM | NUM_AS_TIMESTAMP | DIFF
:---------------------------------- | -----------: | :------------------------------ | ---:
03-SEP-19 09.13.39.989343 PM +01:00 | 190903210000 | 03-SEP-19 09.00.00.000000000 PM | 819
SELECT
systimestamp,
190903210000 num,
TO_DATE(TO_CHAR(190903210000), 'YYMMDDhh24miss') num_as_date,
(
CAST(systimestamp AS DATE)
- TO_DATE(TO_CHAR(190903210000), 'YYMMDDhh24miss')
) * 60 * 60 * 24 diff
FROM DUAL;
SYSTIMESTAMP | NUM | NUM_AS_DATE | DIFF
:---------------------------------- | -----------: | :----------------- | ----------------------------------------:
03-SEP-19 09.20.44.524445 PM +01:00 | 190903210000 | 03-SEP-19 21:00:00 | 1243.999999999999999999999999999999999996
Try this:
select 190903210000 n,
to_timestamp(to_char(190903210000), 'YYMMDDHH24MISS') ts
from dual;
In SQLFiddle and the Oracle documentation.

Time and attendance

I have a table with below data
EMPID | DEVICE | EVENTTIME
-----------------------------------------
112 | READ_IN | 2018-11-02 07:00:00.000
112 | READ_IN | 2018-11-02 08:00:00.000
112 | READ_OUT | 2018-11-02 12:00:00.000
112 | READ_IN | 2018-11-02 13:00:00.000
112 | READ_OUT | 2018-11-02 16:00:00.000
I need a select query to achieve below data:
ID_Emp |Date |TimeIn |TimeOut|Hours
112 |02/11/2018 |8:00 |16:00 |7:00
In my table, the employee came at 7:00 but he didn't do his work then after one hour he came back and work. He took his lunch break at 12:00-13:00 and left his work at 16:00. So his total working hours will be 7 hours.
At first you need to eliminate time between 12 and 1, I wrote simple where clause for this. After that
I used PIVOT for transposing rows to columns by max EVENTTIME.
And finally, I wrote outermost SELECT query for converting columns to your intended format.
here is the fiddler link: http://sqlfiddle.com/#!4/f1189/10
here is the code:
SELECT
EMPID,
TO_CHAR(READ_IN, 'HH24:MI') READ_IN,
TO_CHAR(READ_OUT, 'HH24:MI') READ_OUT,
EXTRACT(HOUR FROM READ_OUT - READ_IN) HOUR
FROM (
select * from (
select * from Table1
WHERE
extract(hour from eventtime) not between '12' and '13'
)
PIVOT (
MAX(EVENTTIME)
for DEVICE in ( 'READ_IN' READ_IN, 'READ_OUT' READ_OUT )
)
)
please note that this example only works for oracle.

How do I subtract two columns with datetime in SQLite?

I am learning SQLite for work and I am trying to subtract 'Enddate' column fromn 'Startdate' column, which contain date and time. Soemthing like this:
Startdate 3/15/18 16:00 3/28/18 17:00
Enddate 3/19/18 00:00 3/20/18 00:00
My table's name is data1. I tried this:
select *,
strftime('%m/%d/%y %H:%M', 'data1.Enddate') -
(strftime('%m/%d/%y %H:%M', 'data1.Startdate')) as TimeOff
from data1;
But this gives me all 'Null' values.
If you could help me with this I would really appreciate that. That you so much!
Two possible reasons you got NULL (likely because of a silent error):
1) Your dates are malformed when you create them. They should be yyyy-mm-dd HH:MM:SS format instead.
2) Not having a closing semicolon in one of your queries. I see it in the one above, but if the one where you insert your test rows didn't close properly, you may not
My test query:
https://dbfiddle.uk/?rdbms=sqlite_3.8&fiddle=8b9a168291bbc08c74a895ce22ab41ac
Setup
CREATE TABLE data1 (foo int, StartDate datetime, EndDate datetime) ;
INSERT INTO data1 (foo, StartDate, EndDate)
VALUES (1,'2018-03-15 16:00:00', '2018-03-28 17:00:00')
, (2,'2018-03-19 00:00:00', '2018-03-20 00:00:00') ;
The Query
SELECT foo, StartDate, EndDate
, julianday(EndDate)-julianday(StartDate) AS TimeOffInDays
, CAST((julianday(EndDate) - julianday(StartDate))*24 AS real) AS TimeOffInHours
FROM data1 ;
Which gives us...
| foo | StartDate | EndDate | TimeOffInDays | TimeOffInHours |
=========================================================================================
| 1 | 2018-03-15 16:00:00 | 2018-03-28 17:00:00 | 13.041666666977 | 313.00000000745 |
| 2 | 2018-03-19 00:00:00 | 2018-03-20 00:00:00 | 1 | 24 |

Add hours to timestamp in Netezza based on other column

I'm working in a Netezza SQL database. I have 2 columns:
DATETIME1: has the timestamp of transaction at UTC time
TIME_OFFSET: is an integer, showing the offset of the users' time zone from UTC. For example, users in UTC+1 have value 1.
What I would like to do is add the TIME_OFFSET column to the DATETIME1 column as an hour.
I created the following reproducible example:
SELECT *
FROM ADMIN.INTEGRAL_ADSCIENCE_1845
WITH my_table AS (
SELECT TIMESTAMP('2017-06-01 08:01:45') AS datetime1,
1 AS time_offset
UNION
SELECT TIMESTAMP('2017-06-01 08:03:45') AS datetime1,
2 AS time_offset
)
SELECT
DATETIME1,
TIME_OFFSET,
DATETIME1 + TIME_OFFSET AS simple_add,
DATETIME1 + INTERVAL '1 hour' AS add_one_hour
FROM my_table;
This creates the following output:
+---------------------+-------------+------------+---------------------+
| DATETIME1 | TIME_OFFSET | SIMPLE_ADD | ADD_ONE_HOUR |
+---------------------+-------------+------------+---------------------+
| 2017-06-01 08:01:45 | 1 | 2017-06-02 | 2017-06-01 09:01:45 |
| 2017-06-01 08:03:45 | 2 | 2017-06-03 | 2017-06-01 09:03:45 |
+---------------------+-------------+------------+---------------------+
But what I would like to have is adding 1 hour to first row and 2 hours to second row.
I'm aware of the mysql date_add() function, but I'm limited to Netezza unfortunately.
This should work for you.
WITH my_table AS (SELECT TIMESTAMP('2017-06-01 08:01:45') AS dttm
, 1 AS tm_offset
UNION
SELECT TIMESTAMP('2017-06-01 08:03:45') AS dttm
, 2 AS tm_offset)
SELECT dttm
, tm_offset
, dttm + tm_offset AS simple_add
, dttm + CAST(tm_offset || ' hour' AS INTERVAL)
FROM my_table;