I have a String column in my database which looks like
07/12/2019 04:17:08 PM
I use the function
cast(from_unixtime(unix_timestamp(myfield, 'MM/dd/yyyy hh:mm:ss'),'yyyy-MM-dd HH:mm:ss') as timestamp)as mytime
This gives me the result of
2019-07-12 04:17:08.0
I want the result to be in utc format and look something like
2019-07-12 16:17:08.
How can i change this to be in utc format?
Use aaa to parse the AM/PM in datetime. from_unixtime converts it to yyyy-MM-dd hh:mm:ss by default where the hour part is 24 hour format.
from_unixtime(unix_timestamp(myfield, 'MM/dd/yyyy hh:mm:ss aaa'))
Related
I have a date column as a string data type in MMMM Do YYYY, HH:mm:ss.SSS
(December 16th 2019, 21:30:22.000) format.
I'm trying to convert this into a timestamp data type in hive but couldn't able to achieve it because this format is not available in unixtime.
Is there any way to convert this in hive?
This method will preserve millisecond precision. First extract only parts compatible with SimpleDateFormat pattern using regex, then convert to datetime, concat with milliseconds (milliseconds lost after unix_timestamp conversion) and convert to timestamp:
select timestamp(concat(from_unixtime(unix_timestamp(dt,'MMM dd yyyy HH:mm:ss.SSS')),'.',split(dt,'\\.')[1]))
from
(select regexp_replace('December 16th 2019, 21:30:22.001','([A-Za-z]+ \\d{1,2})[a-z]{0,2} (\\d{4}), (\\d{2}:\\d{2}:\\d{2}\\.\\d+)','$1 $2 $3') as dt --returns December 16 2019 21:30:22.001
) s;
OK
2019-12-16 21:30:22.001
Time taken: 0.09 seconds, Fetched: 1 row(s)
Try this
SELECT from_unixtime(unix_timestamp) as new_timestamp from data ...
That converts a unix timestamp into a YYYY-MM-DD HH:MM:SS format, then you can use the following functions to get the year, month, and day:
SELECT year(new_timestamp) as year, month(new_timestamp) as month, day(new_timestamp) as day
Calling
SELECT TO_DATE('Mon Sep 22 18:02:41 CDT 2014', 'DY MON DD HH24:MI:SS TZD YYYY') FROM Dual;
I get
ORA-01821: date format not recognized
01821. 00000 - "date format not recognized"
I put together the format string from these Oracle instructions. I tried both "TZR" and "TZD", neither works.
DY Abbreviated name of day.
MON Abbreviated name of month.
DD Day of month (1-31).
HH24 Hour of day (0-23).
MI Minute (0-59).
SS Second (0-59).
TZD Daylight savings information. For example, 'PST'
TZR Time zone region.
YYYY 4-digit year
Why is this not working?
TO_DATE doesn't support timezone.
I think you need to use to_timestamp_TZ() to do what you're after...
SELECT To_TimeStamp_TZ('Mon Sep 22 18:02:41 2014 CDT', 'DY MON DD HH24:MI:SS YYYY TZD')
FROM Dual;
Gives you something like (with my NLS paramaters)
22-SEP-14 06.02.41.000000000 PM AMERICA/CHICAGO
Also note the data type must be TIMESTAMP WITH TIME ZONE Data Type; or oracle just drops the timezone information w/o error.
https://docs.oracle.com/cd/E11882_01/server.112/e10729/ch4datetime.htm#NLSPG238
My Hive table has a date column with UTC date strings. I want to get all rows for a specific EST date.
I am trying to do something like the below:
Select *
from TableName T
where TO_DATE(ConvertToESTTimeZone(T.date)) = "2014-01-12"
I want to know if there is a function for ConvertToESTTimeZone, or how I can achieve that?
I tried the following but it doesnt work (my default timezone is CST):
TO_DATE(from_utc_timestamp(T.Date) = "2014-01-12"
TO_DATE( from_utc_timestamp(to_utc_timestamp (unix_timestamp (T.date), 'CST'),'EST'))
Thanks in advance.
Update:
Strange behavior. When I do this:
select "2014-01-12T15:53:00.000Z", TO_DATE(FROM_UTC_TIMESTAMP(UNIX_TIMESTAMP("2014-01-12T15:53:00.000Z", "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'"), 'EST'))
from TABLE_NAME T1
limit 3
I get
_c0 _c1
0 2014-01-12T15:53:00.000Z 1970-01-16
1 2014-01-12T15:53:00.000Z 1970-01-16
2 2014-01-12T15:53:00.000Z 1970-01-16
Your system timezone CST doesn't matter for converting UTC to EST in Hive. You should be able to get the correct results with:
TO_DATE(FROM_UTC_TIMESTAMP(UNIX_TIMESTAMP(T.date, "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'") * 1000, 'EST'))
Note that because UNIX_TIMESTAMP returns seconds, you will lose the millisecond component of your timestamp.
This converts to CST with the daylight savings hour shift:
to_date(FROM_UTC_TIMESTAMP(UNIX_TIMESTAMP(eff_timestamp, "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'") * 1000, 'CST6CDT'))
How can I convert the string "2014-03-14 15:15:35 PM EST to date format in Oracle by using 'to-date function. Would apprecaite help
The only way I can think of is to ignore "PM":
select to_timestamp_tz('2014-03-14 15:15:35 PM EST', 'YYYY-MM-DD HH24:MI:SS "PM" TZD')
from dual;
15:15:35 PM - the string contains 24-hour format, but at the same time it contains a "PM" indicator. Oracle cannot understand that. Is that 03:15:35 AM or 03:15:35 PM?
I can convert this string to a date
select to_date('2013-10-15T17:18:28', 'YYYY-mm-DD"T"HH24:MI:SS') from dual
But how can I convert this string
'2013-10-15T17:18:28-06:00'
which includes a timezone?
Use TO_TIMESTAMP_TZ instead:
select to_timestamp_tz('2013-10-15T17:18:28-06:00'
,'YYYY-MM-DD"T"HH24:MI:SSTZH:TZM')
from dual;
15/OCT/13 05:18:28.000000000 PM -06:00