Convert epoch number to date and add 01/01/1970 to display the output in HIVE SQL - hive

I am trying hard to figure out how to convert epoch number (column name: effectivedate and datatype: string, value: 19186 ) to date format as mm/dd/yyyy and add '01/01/1970' to display the output in HIVE SQL.
For example: 19186 + 01/01/1970 should display 07/13/2022 as output

The function you are looking for is date_add(). The column effectivedate doesnt look like number of days from 1/1/1970. So using above function you can easily convert it to a full date.
SELECT date_add('1970-01-01', cast(effectivedate as BIGINT))
Test SQL SELECT date_add('1970-01-01', 19186 )

Related

Converting a numeric value into Date value in DB2

I have a DB2 table where NUM column is defined as INTEGER in DB2 and the query result is shown below,
NUM columns have numeric values which needs to be converted to date format. This numeric values are nothing but duration from 01.01.1850. Example : 01.01.1850 + 57677 days = 01.12.2007.
So Is it possible to convert or cast the numeric value into date fields in DB2 , so that the select query from the table can result as shown below after converting a numeric field into date field,
You may use the scalar ADD_DAYS function:
SELECT EMP_ID, ADD_DAYS('1850-01-01', NUM) AS NUM
FROM yourTable;
Not all Db2 products & versions have the ADD_DAYS function.
The following expression works for all of them.
You may optionally add DAY or DAYS at the end.
DATE ('1850-01-01') + 57677

How to convert date (not datetime) to epoch format while running sql query?

So, my table has a date field named batch that looks like this when I do
select batch from my_table;
However, i want this to be converted to the epoch representation, so that all rows contain the epoch value when I select. I tried the following query but it didn't work:
select UNIX_TIMESTAMP(batch) FROM my_table;
So, do i need to convert to datetime first? How do I get this to work?
it does work
select UNIX_TIMESTAMP('2020-01-01')
-- returns 1577833200
select UNIX_TIMESTAMP('2020-01-01 00:00:00')
-- returns 1577833200

Presto/SQL - Converting string timestamp to date throws error

NOTE: I am running my query in Qubole's presto and sql command engine.
I am trying to convert my string timestamp to just date but none of the options are working out.
My string timestamp looks like 2017-03-29 10:32:28.0
and I want to have it like 2017-03-29
I have tried following queries to convert this string timestamp to retrieve date
1. select cast(created as date) from table1
Value cannot be cast to date: 2017-05-26 17:23:58.0
2. select cast(from_iso8601_timestamp(created) as date) from table1
Invalid format: "2014-12-19 06:06:36.0" is malformed at " 06:06:36.0"
3. select date(created) from table1
Value cannot be cast to date: 2012-10-24 13:50:00.0
How I can convert this timestamp to date in presto/sql?
As far as explained in the documentation, prestoDB seems to expect timestamps in a format '2001-08-22 03:04:05.321', and dates in a '2001-08-22'.
One solution would be to use a string function to extract the relevant part of the string before converting it. We know that the date part is located before the first space in the string, so.
If you need the date part as a string datatype:
split_part(created, ' ', 1)
If you need the date part as a date datatype:
cast(split_part(created, ' ', 1) as date)
You can try to use one of the following solutions:
SELECT
'2017-03-29 10:32:28.0' AS input_string,
DATE(date_parse('2017-03-29 10:32:28.0', '%Y-%m%-%d %H:%i:%s.%f')) AS solution_1,
DATE(try_cast('2017-03-29 10:32:28.0' as timestamp)) AS solution_2

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 ;

How to change date format in hive?

My table in hive has a filed of date in the format of '2016/06/01'. but i find that it is not in harmory with the format of '2016-06-01'.
They can not compare for instance.
Both of them are string .
So I want to know how to make them in harmory and can compare them. Or on the other hand, how to change the '2016/06/01' to '2016-06-01' so that them can compare.
Many thanks.
To convert date string from one format to another you have to use two date function of hive
unix_timestamp(string date, string pattern) convert time string
with given pattern to unix time stamp (in seconds), return 0 if
fail.
from_unixtime(bigint unixtime[, string format]) converts the
number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a
string representing the timestamp of that moment in the current
system time zone.
Using above two function you can achieve your desired result.
The sample input and output can be seen from below image:
The final query is
select from_unixtime(unix_timestamp('2016/06/01','yyyy/MM/dd'),'yyyy-MM-dd') from table1;
where table1 is the table name present in my hive database.
I hope this help you!!!
Let's say you have a column 'birth_day' in your table which is in your format,
you should use the following query to convert birth_day into the required format.
date_Format(birth_day, 'yyyy-MM-dd')
You can use it in a query in the following way
select * from yourtable
where
date_Format(birth_day, 'yyyy-MM-dd') = '2019-04-16';
Use :
unix_timestamp(DATE_COLUMN, string pattern)
The above command would help convert the date to unix timestamp format which you may format as you want using the Simple Date Function.
Date Function
cast(to_date(from_unixtime(unix_timestamp(yourdate , 'MM-dd-yyyy'))) as date)
here is my solution (for string to real Date type):
select to_date(replace('2000/01/01', '/', '-')) as dt ;
ps:to_date() returns Date type, this feature needs Hive 2.1+; before 2.1, it returns String.
ps2: hive to_date() function or date_format() function , or even cast() function, cannot regonise the 'yyyy/MM/dd' or 'yyyymmdd' format, which I think is so sad, and make me a little crazy.