Convert UTC timestamp to yyyyMMddHHmmss.SSS format in hive - sql

I have a scenario like below in hive
convert the current_timestamp to UTC. I am able to do so
select to_utc_timestamp(current_timestamp, 'America/Los_Angeles)';
Result:
2020-02-04 10:00:06.162
Next convert this resulting timestamp to yyyyMMddHHmmssSSS format.
I have tried like below
select from_unixtime((to_utc_timestamp(current_timestamp, 'America/Los_Angeles)', 'yyyy-MM-dd HH:mm:ss.SSS'), 'yyyyMMddHHmmssSSS');
I am unable to get the desired result.
expected result is 20200204100006162

You can use the date_format function if the Hive version >= 1.2.0.
select date_format(to_utc_timestamp(current_timestamp, 'America/Los_Angeles'),'yyyyMMddHHmmssSSS')

Related

correct to_char date syntax to have trailing zeroes after milliseconds

My current query in oracle sql for getting a timestamp format is TO_CHAR(c2.start_on,'DD-MM-YY HH:MI:SS.FF PM'), it outputs the timestamp like this 25-11-20 07:00:13.36 PM
However I want it to display the date in this way 25-11-20 07:00:13.360000000 PM
What should I add in the timestamp format for this to be possible ?
I have tried doing it like this HH:MI:SS.FM00000 as suggested here
but it gives me the error. ORA-01821: date format not recognized
what is the correct way to get the date in the desired format ?
If you want fractional seconds, you don't want a DATE, you want a TIMESTAMP. So here's a timestamp formatted with 6 digits of precision
select to_char(systimestamp, 'HH:MI:SS.FF6') from dual;
If you have a date, you could convert it to a TIMESTAMP (using CAST AS TIMESTAMP), but better to look at updating your data model to use the proper type for the source column as starters.

Parse out Y-M-D from Y-M-D H-M-S UTC sql bigquery

I need to parse out '%Y%m%d' from the column in BigQuery. My data looks like this:
datetime_published
2000-09-25 13:28:15 UTC
2018-12-22 16:03:00 UTC
2018-05-04 03:05:00 UTC
I have tried the following:
SELECT PARSE_DATE('%Y%m%d', datetime_published) as date
The error message: No matching signature for function PARSE_DATE for argument types: STRING, TIMESTAMP. Supported signature: PARSE_DATE(STRING, STRING)
Desired output:
2000-09-25
Why not just convert to a date?
select date(datetime)
Note: This works for both datetime and timestamp values. These are different in BigQuery. You have a timestamp column which you have called datetime -- a bit of a misnomer.

AWS Athena (Presto) - how to format Timestamp to Date Format?

I have a column in Athena with Timestamp Data Type and format is: 2019-08-28 00:00:00.000
How to format it to Date format using SQL to be:
DD-MON-YYYY
Thanks.
WITH test AS (
SELECT '2019-08-28 00:00:00.000' AS str
)
SELECT format_datetime(cast(str AS timestamp), 'dd-MM-YYYY')
FROM test
Result:
_col0
1 28-08-2019

In SQL How to convert time into UNIX timestamp

In hive there is some data I have. Now I want to convert the start_timestamp into unix_timestamp in second. How to do that? Because the start_timestamp has two formats:
First format:
2018-03-22 02:54:35
Second format:
May 15 2018 5:15PM
First format is 'yyyy-MM-dd HH:mm:ss', second is 'MMM dd yyyy hh:mm:aa'. If the format is wrong, unix_timestamp function will return NULL. Try to convert using one format, if NULL, try to convert using the other format. This can be done using coalesce function:
select
coalesce(unix_timestamp(start_timestamp ,'yyyy-MM-dd HH:mm:ss'),
unix_timestamp(start_timestamp ,'MMM dd yyyy hh:mm:aa')
) as UnixTimestamp
from my_table;
Use from_unixtime() to convert it back to given format if necessary, like in this answer.
See patterns examples here: SimpleDateFormat

How to convert date in YYYYMMDD in Hive to unix timestamp

I am trying to convert date in format YYYYMMDD in hive to unix_timestamp but when I do below, I am getting incorrect timestamp.
select unix_timestamp(DATE,'YYYYMMDD') from table_name.
For '20180301' I am getting unix timestamp output as '1514631600' which is DECEMBER 30,2017 11:59 pm
The format string should be yyyyMMdd.
select unix_timestamp(DATE,'yyyyMMdd') from table_name