Converting the timestamp in Snowflake - datetime

I'm trying to convert the snowflake timestamp that is compatible with Iterable date format
My attempt:
I have a table with the following timestamps:
|------------------------------|
| table_timestamp |
|------------------------------|
| "2021-07--19 02:45:91.000 Z" |
|------------------------------|
I tried using TIMESTAMP_TZ and I got the following resut
SELECT table_timestamp::TIMESTAMP_TZ
from my_table
gave me output 2021-07--19 02:45:91.000 +0000
How can I get the above timestamp in this format 2021-07--19 02:45:91 +00:00?

You can cast to a varchar and give, as the second parameter, the format that you want:
SELECT TO_VARCHAR('2021-07-19 02:45:31.000'::Timestamp_TZ, 'yyyy-mm-dd hh:mi:ss')
2021-07-19 02:45:31
(Note I changed the seconds to 31 as there isn't 91 seconds in a minute and also changed your double dash between month and day to a single. I'm assuming those were typos)

Give you are correct converting the date/timestamp, this is not a convertion problem, it is a presentation problem.
Thus the Date and Time output formatting help is what you need.

Related

Converting Unix epoch time to extended ISO8601

I have 3 tables I would like to work on using the date, however one of the tables includes the date in unix epoch format. Here is an example of the 3 fields:
Table1: 2017-02-01T07:58:40.756031Z
Table2: 2017-02-07T10:16:46Z
Table3: 1489236559
I would like to convert the date in table 3 to match the format of table 2 as closely as possible. This is what I have right now:
SELECT cast(from_unixtime(tstart) as timestamp) as ISODATE from scada_logs
This gives me something tantalizingly close, but not quite there. Here is what I get:
ISODATE
2017-03-20 14:31:06.000
2017-03-20 14:31:06.000
I've played around with this for a couple of hours but I'm not getting any closer. Does anyone have any ideas?
Thank you!
Option 1: date_format
presto> select date_format(from_unixtime(1489236559),'%Y-%m-%dT%H:%i:%sZ');
_col0
----------------------
2017-03-11T12:49:19Z
Option 2: to_iso8601
presto> select to_iso8601(from_unixtime(1489236559));
_col0
--------------------------
2017-03-11T12:49:19.000Z

How to convert chararray to datetime with milliseconds in pig latin

I wish to convert following value which is a chararray in pig
2016-05-11 23:59:57.628197
to
2016-05-11T23:59:57.628-05:00
How can I do it ?
Following is what I tried considering alias 'a2' contains list of datetime values in chararray in the column named 'input_date_value'
FOREACH a2 GENERATE input_date_value AS input_date:chararray,
ToDate(input_date_value,'YYYY-MM-DD HH:mm:ss.SSSSSS') AS modification_datetime:datetime;
For input -
2002-07-11 16:58:40.249764
Output is -
2002-01-11T16:58:40.249-05:00
The month values like '07' are not getting picked up,
The created timestamp has month set to 01' i.e. January everytime for all dates.
Can someone help. What am I doing wrong ?
https://pig.apache.org/docs/r0.11.1/func.html#to-date ToDate takes SimpleDateFormat only supports milliseconds http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
The -05:00 you see is the time zone ToDate is actually truncating to 3 digits as it supports only milliseconds
Use lowercase character d instead of uppercase D for parsing date values.
Now, I have managed to fix it myself on (In Pig 0.11)
Apparently Pig 0.11 does not support the date format components I used earlier for parsing the month and date.
I found below inference which hints on the incompatibility as mentioned https://www.w3.org/TR/NOTE-datetime
Use:
'YYYY-MM-dd HH:mm:ss.SSSSSS'
instead of 'YYYY-MM-DD HH:mm:ss.SSSSSS'
It now gives correct output.
Input:
2001-11-28 16:04:49.22388
Output:
2001-11-28T16:04:49.223-05:00

SQLite convert date

I have a column of data type TEXT:
date
----
DD/MM/YYYY
but I want to convert all rows in the column to:
date
----
YYYY-MM-DD 00:00:00
(Yes, 00:00:00 for all rows)
Is there any way to do it in SQLite?
Use strftime.
strftime('%Y-%m-%d %H:%M:%S', date_str);
EDIT:
Yes, my first quess do not work. This one does, though:
SELECT
date,
substr(date,7,4)||'-'||substr(date,4,2)||'-'||substr(date,1,2)||' 00:00:00' as text_repr,
datetime(substr(date,7,4)||'-'||substr(date,4,2)||'-'||substr(date,1,2)||' 00:00:00') as datetime_repr
FROM
t
Simply put - You have to parse it on Your own, as stated here or here...

Sort date in sqlite

I want to select the dates in ascending order. Dates are stored in dd-MMM-yy(02-Mar-12) format. Here is my query:
SELECT EventDate,Event,ID from EventCalenderTable Order By EventDate ASC
output is:
10-03-12
12-02-12
15-01-12
18-07-12
But the output should like:
15-01-12
12-02-12
10-03-12
18-07-12
Event Date is date datatype.
I have seen number of post about storing date in sql. I noticed that Convert function done the tricks in sql server. But how can I do this in Sqlite??
Thanks in advance.
SQLite only knows three date formats:
Text ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS")
Real Julian day numbers since November 24, 4714 B.C
Integer number of seconds since 1970-01-01 00:00:00 UTC
SQLite does have five date/time functions for converting between formats.

Julian Day to ISO 8601 string in SQLite

I have a table that stores date/time values as julian days in SQLite (using the julianday() function). I can't seem to figure out how to convert them back to ISO 8601 style strings (YYYY-mm-ddThh:m:ss.sss) when I read them?
Just feed the Julian day number to the datetime function:
A time string can be in any of the following formats:
[...]
12. DDDDDDDDDD
[...]
Format 12 is the Julian day number expressed as a floating point value.
So datetime(julianday_output) goes in the opposite direction as the julianday function:
sqlite> select datetime(julianday(current_timestamp)) as dt_from_jd, current_timestamp as dt;
dt_from_jd | dt
2011-09-30 14:46:52 | 2011-09-30 14:46:52
Have you tried strftime? http://www.sqlite.org/lang_datefunc.html

Resources