Convert Unixtime to MMddyyyy - sql

I'm trying to convert a column which has unixtime (ex 1542862806000) to regular DTS
select unix_timestamp(column_name) from table;
But i get error:
AnalysisException: No matching function with signature: unix_timestamp(BIGINT).
My column type is bigint

You are looking for from_unixtime not unix_timestamp.
select from_unixtime(cast(column_name/1000 as bigint),'MMddyyyy')
from table
unix_timestamp converts a date/date format string to a bigint representing the number of seconds since 1970-01-01 00:00:00 UTC.
from_unixtime takes a bigint input and converts it to the required date format.

Related

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.

Convert timestamp to datetime changing format

I have a timestamp 2020-01-08T16:06:00+00:00 format value, I want to convert it to datetime format as 2020-01-08 16:06:00.
I have tried to convert to datetime, but getting error says:
failed when converting date and/or time from character string.
I would like to convert timestamp to datetime.
If you don't care about the timezone, you can use:
select convert(datetime, left('2020-01-08T16:06:00+00:00', 19))
If you're simply interested in dropping the the offset, and not converting the timestamp to local time (as the offset sets the timestamp at UTC), try this:
DECLARE #timestamp AS DATETIMEOFFSET = '2020-01-08T16:06:00+00:00' ;
SELECT CAST ( #timestamp AS DATETIME2(0) ) AS formatted_timestamp ;
(I've assumed the data type for your timestamp is DATETIMEOFFSET. But this could also work if the data type is DATETIME or DATETIME2.)

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.

extract the date from a timestamp value variable in Impala

How can I extract the date from a timestamp value variable in Impala?
eg time = 2018-04-11 16:05:19 should be 2018-04-11
try this:
from_timestamp('2018-04-11 16:05:19','yyyy-MM-dd') as date_value
from_timestamp(datetime timestamp, pattern string): Converts a TIMESTAMP value into a string representing the same value. Please see documentation here
to_date (t1.local_time), as date_value
to_date: Returns a string representation of the date field from a timestamp value.

how to convert a timestamp to int in sql (vertica)

I have a timestamp as 2017-07-19 11:45:01and i want it to convert to int.
Query:
select cast(max(event_timestamp) as INT) from error_messages where error_level='ERROR' and user_name='git'
Error:
SQL Error [2366] [42846]: [Vertica][VJDBC](2366) ERROR: Cannot cast type timestamptz to int
[Vertica][VJDBC](2366) ERROR: Cannot cast type timestamptz to int
com.vertica.util.ServerException: [Vertica][VJDBC](2366) ERROR: Cannot cast type timestamptz to int
You have to use TIMESTAMPDIFF() this way:
SELECT TIMESTAMPDIFF(SECOND,'001-01-01 00:00:00', '2015-02-23 03:12:35');
timestampdiff
---------------
63560257955
to get the number of time units you want (SECONDs here above) since the timestamp you want...
If you want to get Unix Timestamp of that date as int than search fort that.
One option would be to calculate the range from your date to '1970-01-01' in seconds as int. This is the Unix Timestamp.
Use JULIAN_DAY function in Vertica to convert the time stamp to a integer value or number.
For more details refer Vertica documentation link: https://my.vertica.com/docs/6.1.x/HTML/index.htm#16070.htm
To extract number from date time with 1 second interval.
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-08');