how to process text timestamp in hive - hive

I have a column in hive table stored as text. The text looks as shown below
2007-01-01T00:00:00+00:00
I am trying to find difference in time between two timestamp value stored as text in the above format.

Suppose we've got an Hive table dateTest with two column date1 string, date2 string
and suppose that table containing a row with this values:
2007-01-01T00:00:00+00:00,2007-02-01T00:00:00+00:00
The dates are in ISO 8601 UTC format, so if you run this query:
select datediff(from_unixtime(unix_timestamp(date2, "yyyy-MM-dd'T'HH:mm:ss")),from_unixtime(unix_timestamp(date1, "yyyy-MM-dd'T'HH:mm:ss"))) as days
from datetest;
the result is 31

Related

Converting VarChar Column with multiple date format to single date column with proper format

I have data of transaction dates in one column within varchar format.
This column contains three separate formats of date in dd-mm-yy, dd/dd/yyyy and third format in dd/mm/yyy format with missing the last digit.
How to modify this column in the correct date format column with the dd-mm-yyyy format.
Add a date column and fill with your convert statement to get the desired format. Once everything looks okay you can drop the text column and rename the new proper date column to match the original.

Conversion of timestamp/varchar column to bigint in presto which contains two different date formats

I have a source table click with a column named click_date whose datatype is varchar, it contains values with two different format of dates in the same column. For example:
Jul 17 2018 4:54PM
2019-02-05 08:20:29.000
I have a target table named click and I need to map the data into it in a column named click_date whose datatype is bigint.
So it is throwing an error while doing casting
Tried the below :
td_time_parse(cast(cast(click_date as timestamp)as varchar))
But it doesn't solve both the formats.. I need to convert both the date formats into integer so that it can be loaded into the target.
note that target data type cannot be changed and it is bigint. Фny leads will be appreciated.
You could use a combination of date_parse and to_unixtime to convert your date in varchar into a unixtimestamp (which is in double datatype).
for date format: 2019-02-05 08:20:29.000
to_unixtime(date_parse('2019-02-05 08:20:29.000','%Y-%m-%d %h:%i:%s'))
for date format: Jul 17 2018
to_unixtime(date_parse('Jul 17 2018','%b %d %Y'))
This will return you a double datatype of your date which you can store it in your database.
You can use below query to handle both formats of dates with one expression. It essentially looks if the date/timestamp starts with a numeric or not.
select
if(click_date rlike '^[0-9]', unix_timestamp(click_date ,'yyyy-MM-dd HH:mm:ss'), unix_timestamp(click_date,'MMM dd yyyy')) as click_date_mod
from click;

Null result when converting String to Date format in Databricks SQL

I have a String column from a temp view, were dates values are stored in format of '2020/01/01'.
I do need to convert that field to a date value to be stored into a Delta table in 'YYY-MM-DD' format.
I've been trying different kind of SQL functions but none of them seems to works, as a result, every function retrieves a null value:
select to_date(dateField, 'yyyy-MM-dd'),
CAST(dateField as date),
etc..
from TMP_table
Every function results in a null value, and there's no information about that into Databricks documentation.
As Usagi said above, select to_date(dateField, 'yyyy/MM/dd') AS date works perfectly.

Hive table date column value converson

in Hive table value for one column is like 01/12/17.But I need the value in the format as 12-2017(month-year).How to convert it?
Convert the string to a unix_timestamp and output the required format using from_unixtime.
select from_unixtime(unix_timestamp(col_name,'MM/dd/yy'),'MM-yyyy')

How to convert a binary date column date into gregorian date format in sql

This i got to convert a single binary date to Gregorian date ,
Select to_char(to_date(sum(2415020+42744),'J'),'MMDDYYYY') Last_arrear_date from dual;
But I'm not able to convert a binary date column date into Gregorean in xyz table
For Example
Suppose i have a table borm,That has account_open_date column that stores data in binary format , I want to convert that account_open_date column data into Gregorian date format. Please let me know how to convert that
While you have to change the entire column, Don't use the sum function.
Try the below SQL command it will work.
select acct_no, to_char(to_date((account_open_date + 2415020), 'J'),'DD-MON-YYYY')
from borm;
To Change Binary to Gregorian
select column1 ,...columnN, to_char(to_date((Column_name + 2415020), 'J'),'DD-MON-YYYY')
from Table_Name
WHERE Condition;
To change Gregorian to Binary:
select column1,columnN, to_char(to_date(Column_Name,'YYYYMMDD'),'J')-2415020
from Table_Name
where condition ;