In SQL How to convert time into UNIX timestamp - sql

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

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.

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 the date July 1, 2017 to dd-MM-yyyy using Hive SQL?

I have a Hive table with a Week column having values such as:
I have to convert this field to a date format such as: 2017-07-01 (yyyy-MM-dd) using hive SQL.
Any suggestions?
You can use a combination of from_unixtime and unix_timestamp.
select from_unixtime(unix_timestamp(weekCol,'MMM dd, yyyy'),'yyyy-MM-dd')
Use a combination of unix_timestamp and from_unixtime
select from_unixtime(unix_timestamp(week,'MMMM dd, yyyy'),'yyyy-MM-dd') from table_name;
unix_timestamp(string datetime, string pattern) converts datetime with given pattern to unix time stamp.
from_unixtime(bigint unixtime[, string format]) converts the number of seconds from unix epoch.

TIMESTAMP in hive?

I have one column , data as 'Apr 06 2016 05:30:30' it is not in the time stamp formate, when using this one as timestamp I am getting null values. So stored as string, now I want to do some calculation on this when it is in time stamp formate. for that i converted into unixtimestamp and getting back to timestamp formate but the value of the date is changed. I used conversions as 'select from_unixtime(unix_timestamp(start_time, 'MMM DD YYYY HH:mm:ss')) from temp;'
I got value as '2015-12-27 05:30:30'.
I want final data as '2016-04-06 05:30:30'.
Please help me on this
You have just written the wrong format. The proper format string is 'MMM dd yyyy HH:mm:ss'. Take a look at https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html for reference for format strings.

SQLite Data Time Function

I currently have a timestamp in this format Tue Jun 03 17:17:05 +0000 2014 in one column in my table. I want to count the number of records happening in specific intervals (15 minutes). I have tried to follow the answer found in Group records by time. Although my timestamp is in a different format and I haven't seen any support function available in SQLite to convert this. Is this possible in SQL?
The SQLite date and time functions can be used to convert a timestring to a canonical format, or to a Julian Day Number. Unfortunately, the SQLite date and time functions only accept timestring in a limited number of formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
If your timestring format has fixed field widths, you can use the substr function and the || string concatenation operator to convert it to a format SQLite understands. You'll have to use a case expression to convert the month names to numbers; here's an example.
You may use NEW_TIME in Oracle to convert the time to a specific timezone. Here is an example. This example is converting SYSDATE from PDT to GMT.
SELECT NEW_TIME (SYSDATE, 'PDT', 'GMT') FROM DUAL;
This thread is detailing how to add required minutes to your timestamp.