Conversion of datetime format - sql

I have column name requestdatetime with data type string.
Value for requestdatetime is in format 15/Aug/2022:01:54:41 +0000
I need to convert 15/Aug/2022:01:54:41 +0000 into 'yyyy-MM-dd HH:mm:ss' format.
I have tried date_parse(requestdatetime,'%d/%b/%Y'':''HH:mm:ss'' ''+SSS') but it not working out.

You need to convert string to date then date to string to get expected result.
select date_format(parse_datetime('15/Aug/2022:01:54:41 +0000','dd/MMM/yyyy:HH:mm:ss Z'), '%Y/%m/%d %T')
result:
2022/08/15 01:54:41

date_parse accepts MySQL date format, try parse_datetime which accepts Java format (do not forget to add part for timezone offset - Z):
SELECT parse_datetime('15/Aug/2022:01:54:41 +0000', 'dd/MMM/yyyy:HH:mm:ss Z');
Output:
_col0
2022-08-15 01:54:41.000 UTC

Related

How to convert string with GMT to date in Athena

I have a column filled with dates in string format, e.g. 2023-01-31 11:21:33 GMT.
I am trying to write a query that will select a year and a month and will do some calculations later on. My standard approaches using EXTRACT(YEAR FROM a)) etc. did not work. Therefore, I am trying to parse datetime using PARSE_DATETIME(a, 'yyyy-mm-dd hh:mm:ss'). The thing is, I don't know how to format "GMT" and google did not help with that.
The error message is INVALID_FUNCTION_ARGUMENT: Invalid format: "2023-01-31 11:21:33 GMT" is malformed at "GMT".
Use 'yyyy-MM-dd HH:mm:ss z':
select parse_datetime('2023-01-31 11:21:33 GMT', 'yyyy-MM-dd HH:mm:ss z')
Output:
_col0
2023-01-31 11:21:33.000 UTC
parse_datetime is Java date function which uses JodaTime’s DateTimeFormat pattern format which is mostly compatible with java.text.SimpleDateFormat with z matching general timezone.

Impala: convert dd-mm-yy hh:mm:ss string to Date yyyy-mm-dd HH:mm:ss.SSS format

I have a impala table where date column values are stored in dd-mm-yy HH:mm:ss string format, e.g.
30-11-20 12:34:45
I want to convert them into yyyy-mm-dd HH:mm:ss.SSS, e.g.
2020-11-30 12:34:45.000
If anyone could suggest a way to achieve this!!
first use to_timestamp to convert to timestamp. Then use from_timestamp to convert to string.
select from_timestamp(
to_timestamp('30-11-20 12:34:45','dd-MM-yy HH:mm:ss')
,'yyyy-MM-dd HH:mm:ss.SSS') as str_timestamp

How to pass format to to_timestamp in postgresql?

I have utc epocha in millisecond and I would like my sql return resulting dates in a specific date format.
This works
SELECT to_timestamp(timestamp / 1000) as date
FROM data
This does not work
SELECT to_timestamp(timestamp / 1000, 'YYYY-MM-DD HH:MI:SS') as date
FROM data
Could you tell me what is wrong in my script and how to fix it please? Thanks
to_timestamp() does not format your output. The version where to_timestamp() accepts a format mask is used to convert a string value to a proper timestamp.
You need to format the result of your conversion (which is a proper timestamp) using to_char().
to_char(to_timestamp(timestamp / 1000), 'yyyy-mm-dd hh24:mi:ss') as date

How to convert this time to hive timestamp

I have a timestamp value as below:
20171020T222028.026 GMT
I want to convert this to the following format:
2018-12-01 19:35:05
Is this possible in Hive ?
Input - 20171020T222028.026 GMT
Output - 2017-10-20 22:20:28
The input format needs to be parsed as shown here. No format parameter is needed for from_unixtime as the expected output is in the default output format yyyy-MM-dd HH:mm:ss
select from_unixtime(unix_timestamp('20171020T222028.026 GMT',"yyyyMMdd'T'HHmmss.SSS z"))
Formats:
'T' to escape the literal in the input value.
z for Timezone

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