Snowflake:Epoch to Timestamp - snowflake-cloud-data-platform

Requirement: Convert Epoch to Timestamp
Issue: It is converting into local timezone
When I have checked manually at online it is as below
When I tried in Snowflake it is as
Expected output is
2017-12-15 09:21:15 | 2017-12-19 14:21:59

The easiest way to get a timestamp from epoch is TO_TIMESTAMP(). But if you also want to work with timezones, you'll need a timestamp with timezone. For that, use TO_TIMESTAMP_TZ().
SELECT TO_TIMESTAMP(1513347675), TO_TIMESTAMP(1513711319)
, TO_TIMESTAMP_TZ(1513347675), TO_TIMESTAMP_TZ(1513711319)
# 17-12-15 14:21
# 17-12-19 19:21
# 17-12-15 09:21
# 17-12-19 14:21
--
Complete example:
ALTER SESSION SET TIMESTAMP_TZ_OUTPUT_FORMAT = 'YY-MM-DD HH24:MI TZHTZM';
ALTER SESSION SET TIMEZONE = 'Etc/GMT';
SELECT TO_TIMESTAMP_TZ(1513347675);
-- 17-12-15 14:21 +0000
ALTER SESSION SET TIMEZONE = 'US/Eastern';
SELECT TO_TIMESTAMP_TZ(1513347675);
-- 17-12-15 09:21 -0500
ALTER SESSION SET TIMEZONE = 'US/Pacific';
SELECT TO_TIMESTAMP_TZ(1513347675);
-- 17-12-15 06:21 -0800

Related

timestamp "2022-01-02T12:30:30.471395746-06" is not recognized snowflake

I have a timestamp that looks like this:
2022-01-02T12:30:30.471395746-06
I tried running the following code:
alter session set timestamp_input_format = 'AUTO';
select s.$1, s.$2, to_timestamp_tz(s.$3), s.$4, s.$5
from #my_stage s limit 10;
This gives me an error saying:
Timestamp '2022-01-02T12:30:30.471395746-06' is not recognized
I also tried setting the format to
alter session set timestamp_input_format = 'YYYY-MM-DD"T"HH24:MI:SS.FF9';
alter session set timestamp_output_format = 'YYYY-MM-DD"T"HH24:MI:SS.FF9';
which did not work. Any suggestion would be appreicated!
You are missing the timezone part at the end, it should be specified in the timestamp format as TZH in your case.
The following should work:
alter session set timestamp_input_format = 'YYYY-MM-DDTHH24:MI:SS.FF9TZH';
or
select to_timestamp_tz('2022-01-02T12:30:30.471395746-06', 'YYYY-MM-DDTHH24:MI:SS.FF9TZH');
Output:
2022-01-02T12:30:30.471395746-06:00

PostgreSql cron.job set statement_timeout not work

I'm using RDS with PostgresSql 12.6, trying to figure out why set statement_timeout not work in cron.job
This is my cron.job query:
SELECT cron.schedule('* * * * *', $$set statement_timeout = '1min';select pg_sleep(5 * 60);$$);
When I check job status from:
select * from cron.job_run_details
The output are below:
jobbed
runid
job_pid
database
username
command
status
return_message
start_time
end_time
8
1318621
3255
postgres
postgres
set statement_timeout = '1min';select pg_sleep(5 * 60);
failed
ERROR: canceling statement due to statement timeout
2022-05-27 04:05:00.006857 +00:00
2022-05-27 04:05:30.009318 +00:00
I found range between end_time and start_time is 30s. Same as global parameter.
SELECT current_setting('statement_timeout'); -- show 30s
Is there any solution for making cron.job work without change global parameter?

How to construct timestamp_tz from timestamp_ntz using time zone names

In Snowflake how do I combine a time zone name with a timestone_ntz field to create a timestamp_tz value?
In Oracle it's:
SELECT from_tz(<timestamp_col>, <timezone_name>)
FROM <my_table>;
All the Snowflake examples I have seen seem to require me to specify the time zone as an offset in terms of hours and/or set a session parameter.
I don't want to have to specify time zone offsets in hours. I don't wish to use session parameters as I want to control the loading at field/record, not session, level.
I think something along the lines of this does what I want but it seems there must be a better way:
SELECT convert_timezone('Europe/London', 'Europe/London', <timestamp_ntz_col>);
I also am aware of the timestamp_tz_from_parts() function, but that seems a bit 'heavy'.
Is there a simpler way?
I also am aware of the timestamp_tz_from_parts() function, but that
seems a bit 'heavy'.
I don't think it's heavy, and you can create your own function to reduce the complexity of your main queries:
create or replace function mergetz( ts varchar, tz varchar )
returns timestamp_tz as
$$
TIMESTAMP_TZ_FROM_PARTS(
YEAR( ts::date ),
MONTH( ts::date ),
DAY( ts::date ),
HOUR( ts::datetime ),
MINUTE( ts::datetime ),
SECOND( ts::datetime ),
0, -- nanoseconds
tz )
$$
;
SELECT mergetz( ts, tz)
from values
('2019-01-01 12:00:00','America/New_York'),
('2019-01-01 12:15:00','Europe/London') tmp (ts, tz );
+-------------------------------+
| MERGETZ( TS, TZ) |
+-------------------------------+
| 2019-01-01 12:00:00.000 -0500 |
| 2019-01-01 12:15:00.000 +0000 |
+-------------------------------+

Date error in DateProperty() save via ndb

Strange results on a saved date in GAE (Python with ndb library).
The inbound string from a web form is %a %m/%d/%y formatted (Fri 3/3/17).
That's parsed with datetime.strptime to get a date value.
When it's saved in a DateProperty() field, the value is consistently the day before, at 16:00:00.000 PST.
postDate = datetime.datetime.strptime(self.request.get('date-'+
hashed_id),'%a %m/%d/%y')
logging.info('postDate as Date: %s',postDate)
postDateStr = datetime.datetime.strftime(postDate,'%D')
logging.info('postDateStr: %s',postDateStr)
thisPost = ScheduledPost(id = postID,
...
postDate = postDate,
postDateStr = postDateStr
)
log results:
postDate as Date: 2017-03-03 00:00:00
postDateStr: 03/03/17
so far so good, right? but in the Datastore interface, that record shows:
PostDate: 2017-03-02 (16:00:00:000 PST)
PostDateStr: 03/03/17
Oops.
Workstation is in Pacific time - but leaving that aside - date queries seem to confirm that the date is wrong. Assuming today is 3/3/17 -
today = dt.datetime.now()
ScheduledPost.query(ScheduledPost.postDate == today).fetch()
No record returned.
Saving date as string, and querying on date as string, are feasible workarounds for this project. Just thought it merited posting - anyone seen this? Advice?
The datetime you're generating on appengine is UTC, and it gets stored as that datetime, without timezone information.
When you're viewing it, it's being converted to pacific-time.
The query you're doing is incorrect: you're generating a datetime, not a date, so the time you're comparing with is going to be different.

Sybase dataadd time where time added is the difference of 2 times

I have a sybase database and would like to create a new bigdatetimefield by adding time to a current bigdatetimefield
for example
I have a date1 field = 8/31/2015 2:23:49.529000 PM
I have a date2 field = 8/31/2015 7:23:49.529000 AM
I have a mainDate field = 8/31/2015 2:24:46.112000 PM
I would like to make a new field that is the mainDate field minus the difference in time between the date1 field and the date2 field
So in this case the new filed would be 8/31/2015 2:24:46.112000 PM - (8/31/2015 2:23:49.529000 PM - 8/31/2015 7:23:49.529000 AM)
Any idea how to do that in sybase?
SELECT new_dt = DATEADD(ss, datediff(ss,date1,date2),mainDate)
FROM my_table
That's only accurate to seconds though. You can use millisecond or microsecond.

Resources