How to get hive date at start of day? - hive

Given a date : 2016-01-08 12:15:12
I would like to transform it to 2016-01-08 00:00:00
How can I get this result in hive ?

Use the substr function to get the date and then concatenate the 00:00:00.
select concat(substr('2016-01-08 12:15:12',1,10),' 00:00:00')

trunc(timestamp, string unit)
Source: trunc
Purpose: Strips off fields and optionally rounds a TIMESTAMP value.
If you cast the result of TRUNC() to STRING, you will often see zeroed-out portions such as 00:00:00 in the time field.
cast(trunc(2016-01-08 12:15:12,'HH') as varchar(20))

Related

How do I extract a date (dd-mm-yy) from a timestamp with timezone (timestamptz) in postgresql

My date column "timestamp" is currently listed as:
2020-11-16 20:27:38.033 +0000
It's formatted as timestamptz and I've tried every search on here and google to find a method to only pull the date part (in this example 2020-11-16) from the column so I can effectively start grouping data by Date.
Any help would be greatly appreciated.
Assuming (as you haven't stated) that the column is a string. This shows how to convert:
postgres=# SELECT ('2020-11-16 20:27:38.033 +0000'::timestamp)::date;
date
------------
2020-11-16
If it were already a timestamp, then just the ::date cast would work.
You can use ::DATE casting or use TO_CHAR() conversion if the aim is just to display in that format
such as
SELECT your_ts_column::DATE AS val_as_date,
TO_CHAR(your_ts_column, 'YYYY-MM-DD') AS val_as_str
FROM your_table
Demo

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.

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.

Hive date conversion

I intended to extend certain records in a table by adding 366 days to its date keys:
to_date(date_add(from_unixtime(unix_timestamp('20150101' ,'yyyyMMdd'), 'yyyy-MM-dd'), 366)) as new_date
2016-01-01
But how to convert this value back to format of original key i.e. 20160101 ?
Since your requested date is 2016-01-01 it seems you want to add 365 days and not 366.
select from_unixtime(unix_timestamp(date_add(from_unixtime(unix_timestamp(
'20150101','yyyyMMdd')),365),'yyyy-MM-dd'),'yyyyMMdd');
Demo
hive> select from_unixtime(unix_timestamp(date_add(from_unixtime(unix_timestamp(
> '20150101','yyyyMMdd')),365),'yyyy-MM-dd'),'yyyyMMdd');
OK
20160101
date_add gives you a date type as output.
As you have already used from_unixtime and unix_timestamp, I'll assume that you are already aware of their functionalities.
In Hive/Impala, there's no native DATE_FORMAT function like MySQL/MariaDB, so you'll have to convert the output of your date_add to unix_timestamp and then use from_unixtime on the output to achieve the desired format.
Something along the lines of:
select
from_unixtime(unix_timestamp(
date_add(from_unixtime(unix_timestamp('20150101' ,'yyyyMMdd')), 365)),
'yyyyMMdd');

Hive's unix_timestamp and from_unixtime functions

I am under the impression that unix_timestamp and from_unixtime Hive functions are 'reverse' of each other.
When I try to convert timestamp string to seconds in Hive:
SELECT unix_timestamp('10-Jun-15 10.00.00.000000 AM', 'dd-MMM-yy hh.mm.ss.MS a');
I get 1418176800.
When I try to convert 1418176800 to timestamp string:
SELECT from_unixtime(1418176800, 'dd-MMM-yy hh.mm.ss.MS a');
I get 10-Dec-14 10.00.00.120 AM, which is obviously not equal to the original.
Can someone explain what's going on? Thanks.
From the language manual:
Convert time string with given pattern to Unix time stamp (in seconds)
The result of this function is in seconds.
Your result changes with the milliseconds portion of the date, but the unix functions only support seconds. For example:
SELECT unix_timestamp('10-Jun-15 10.00.00 AM', 'dd-MMM-yy hh.mm.ss a');
1433930400
SELECT from_unixtime(1433930400, 'dd-MMM-yy hh.mm.ss a');
10-Jun-15 10.00.00 AM
Don't use from unix_timestamp as you have in your second query. Additionally your statement has a formatting in it that gives the result where DEC is used in steads of 12. See dd-MM-yy. Don't specify a format and it should work. See the examples below.
You are however correct that from_unixtime() and unix_timestamp() are used to convert back and forth from time string.
select unix_timestamp('2015-04-09 03:04:26') from dual;
results in "1428566666"
select from_unixtime(1428566666) from dual;
results in "2015-04-09 03:04:26"