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

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

Related

SQL, Converting date timestamp variable into strings 'yyyymmdd' and 'yyyymm'

I would like to replace a date timestamp variable 'date' with the format yyyy-mm-dd hh:mm:ss e.g, 2021-12-28 00:00:00
with two other string variables; one named date with the format: 'yyyymmdd' e.g, 20211228, and one named month with the format: 'yyyymm' e.g, 202112.
Can you give me some suggestions using a SELECT statement?
Thanks
Just use TO_CHAR (or whatever the equivalent is for your DBMS) and the appropriate format string

Impala convert timestamp AM/PM

I have table in hive with string in 12 hrs format
in which data is appended every day. I don't have control over hive table. Need to expose as view in impala after conversion.
to_timestamp() doesn't support AM/PM format.
Do we have a better way to convert string in 12 hrs format to timestamp.
came across this function which supports AM/PM.which support ISO SQL:2016 standard patterns.
CAST(expression AS type FORMAT pattern)
impala conversion functions
This function is not supported by the impala version I am using.
The other ways I came across:
To add 12hrs to hh:mm if it is PM.
Convert it to 24hrs format in Hive to other table or view using unix_timestamp and have it through impala(require to expose table as view in impala)
can you use below function?
This will give you from string with am/pm to a date time format data.
from_unixtime(unix_timestamp(string_with_ampm, 'MM/dd/yyyy hh:mm:ss a'))
Example
select from_unixtime(unix_timestamp('01/25/2021 09:58:37 AM', 'MM/dd/yyyy hh:mm:ss a')) c
union
select from_unixtime(unix_timestamp('01/25/2021 07:58:37 PM', 'MM/dd/yyyy hh:mm:ss a'))
Result

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

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 do i convert date time format in informatica

I want to map a datetime mm/dd/yyyy hh24:mi:ss attribute in flatfile to Teradata table date yyyy-mm-dd attribute using informatica.
When I added to_date(date_field, 'yyyy-mm-dd') I'm encountering oracle fatal error. When I tried with to_date(to_char(date_field, 'yyyy-mm-dd')) it is giving invalid string input to to_date().
Can anyone help?
In Informatica, the format that you specify under to_date() function should be same as your source data format and not your target format.
So in your case, to_date function should be like this:
to_date (date_field, 'mm/dd/yyyy hh24:mi:ss')
This is because your flat file date has a format of mm/dd/yyyy hh24:mi:ss (Ensure that all the records in this column in your flat file date are really in this format - else you will encounter error)
Do not worry about target date format as long as the target column is a date datatype. By nature, date datatype does not have a format, it's only the display that needs format.