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

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

Related

BigQuery doesn't convert some string values to date

My table has its dates in string format. I'm trying to transform them to date, but BQ is not accepting some values and I can't find the reason.
What I've tried:
PARSE_TIMESTAMP('%Y%m%d', CLIENTE.dtnasc)
PARSE_TIMESTAMP('%Y%m%d', cast( CLIENTE.dtnasc as string))
CAST(CLIENTE.dtnasc AS DATE)
Error example (it's not only this value):
Failed to parse input string "1991-02-11 00:00:00"
How the date is without transfomartion:
2000-01-01 00:00:00 (string)
Try this:
SELECT PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', '1991-02-11 00:00:00')
SELECT PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', CLIENTE.dtnasc)
FROM mytable
Consider below option (the simplest I can think of, having that your string column matches already format of timestamp - 2000-01-01 00:00:00 (string))
SELECT DATE(TIMESTAMP(CLIENTE.dtnasc))

Convert UTC timestamp to yyyyMMddHHmmss.SSS format in hive

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')

Change Date Format from an array in SQL SELECT Statement

I have a column updated_at that returns an array
["2019-01-05T17:28:32.506-05:00","2019-06-15T13:22:02.625-04:00"]
But I want the output date format like this 2019-01-03.
How can I accomplish this in sql databricks?
Thanks!
Try unnest and cast that as a date:
with ts_array as
(select array['2019-01-05T17:28:32.506-05:00','2019-06-15T13:22:02.625-04:00'] as tsa)
select unnest(tsa)::date from ts_array ;
You can use "date_trunc" SQL function to get the output in date format.
date_trunc(fmt, ts) - Returns timestamp ts truncated to the unit specified by the format model fmt. fmt should be one of [“YEAR”, “YYYY”, “YY”, “MON”, “MONTH”, “MM”, “DAY”, “DD”, “HOUR”, “MINUTE”, “SECOND”, “WEEK”, “QUARTER”]
Examples:
> SELECT date_trunc('YEAR', '2015-03-05T09:32:05.359');
2015-01-01 00:00:00
> SELECT date_trunc('MM', '2015-03-05T09:32:05.359');
2015-03-01 00:00:00
> SELECT date_trunc('DD', '2015-03-05T09:32:05.359');
2015-03-05 00:00:00
> SELECT date_trunc('HOUR', '2015-03-05T09:32:05.359');
2015-03-05 09:00:00
Reference: Databricks - SQL Functions.
Hope this helps.

Convert DATETIME format to custom format in bigquery

I have a column in BigQuery in DATETIME eg 2018-08-16T11:00:35.683000 which I would like to convert in some custom format which is YYYY-MM-dd HH:MM.
How can I do this ?
I am getting below error while converting:
ValueError: timestamp out of range for platform localtime()/gmtime() function
Below example for BigQuery Standard SQL
#standardSQL
WITH `project.dataset.table` AS (
SELECT DATETIME '2018-08-16T11:00:35.683000' dt
)
SELECT FORMAT_DATETIME('%Y-%m-%d %R', dt) cust_dt
FROM `project.dataset.table`
with result
Row cust_dt
1 2018-08-16 11:00

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