GreenPlum SQL to HiveSQL migration of query statement - hive

We have to migrate the GreenPlum SQL to HIVESQL as below statement, kindly help us.
to_date(b.birthday,'yyyymmddhh24miss')
extract(year from age(iDATE, to_date(b.birthday,'yyyymmddhh24miss')))
Above the two statement needs to convert SQL to HIVE. please help me.
hive version : 3.1.X

Equivalent of to_date(b.birthday,'yyyymmddhh24miss')
is
from_unixtime(unix_timestamp(b.birthday,'yyyyMMddHHmmss'))
However, i do not understand this extract(year from age(iDATE, to_date(b.birthday,'yyyymmddhh24miss'))). Do you need difference of iDate and birthdate ?
If yes, you can use to get date difference in years.
datediff(iDate,from_unixtime(unix_timestamp(b.birthday,'yyyyMMddHHmmss')) )/365
Hive also has extract (year from datetimestamp) functions.

One more method of extracting timestamp is using regexp_replace:
regexp_replace(b.birthday,'^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})$','$1-$2-$3 $4:$5:$6')
And age in years is:
cast(datediff(current_date,
regexp_replace(b.birthday,'^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})$','$1-$2-$3 $4:$5:$6')
)/365.25 as int)

Related

Get the number of days between two dates

I'm working on JupyterLab(SQL) and I want to get the difference of days between two columns.
The values of the columns are in the format YYYYMMDD but they aren't integers
How can I transform the columns to dates and then get the differences of days.
I'm not totally sure about JupyterLab, but in SQL Server you can use DATEDIFF() to calculate this. For example:
SELECT DATEDIFF(day, '2017/08/25', '2011/08/25') AS DateDiff;
See also:
https://www.w3schools.com/sql/func_sqlserver_datediff.asp
You did not mention the dbms so i am answering for oracle and postgres. You can use above answers but i like to convert them explicitly befor calculating difference.
so here is for
oracle -
TO_DATE('20170103','YYYYMMDD') - TO_DATE('20200103','YYYYMMDD')
postgres
EXTRACT(DAY FROM TO_TIMESTAMP('20160101', 'YYYYMMDD')-TO_TIMESTAMP('20150301', 'YYYYMMDD')
JupyterLab(SQL) supports SQLite, PostgreSQL, and MySQL databases:
MySQL:
SELECT DATEDIFF("20201005", "20201001");
PostgreSQL:
SELECT DATE_PART('day', AGE('20201005', '20201001'));

SQL - truncating (Oracle) dates - what is a future safe option?

We are using the Oracle TRUNC() sql function to truncate dates. The column has type DATE.
What is a good and future proof option for truncating dates? Chances are that we are going to migrate to Postgress or MySql.
Example query:
SELECT *
FROM KIB_IND_SNAPSHOT
WHERE snsh_id IN (SELECT MAX(snsh_id)
FROM KIB_IND_SNAPSHOT
WHERE SNSH_STATUS = 'SUCCES'
GROUP BY TRUNC(SNSH_TIJDST_ONTST)
)
ORDER BY SNSH_TIJDST_ONTST;
Follow Universal date format i.e. YYYY-MM-DD which will future proof it(as of now :) ).Please refer following URL https://www.w3.org/TR/NOTE-datetime for details.
Update : BTW the answer I gave is for scenario where you want to migrate your DB to other Database. Following this will give you less headache

SQL - subtract between two dates

I'm trying to find the number of days between two date columns.
I tried to use DATEDIFF but I got an error. What more should I do
Thanks,
Are you sure you are using the correct SQL syntax for your database? Since you're using MySQL, you need to do a
SELECT DATEDIFF('2015-06-05', '2015-08-05');
and the difference is always expressed in days.
On SQL Server you need to specify the unit e.g.
SELECT DATEDIFF(day, '2015-06-05', '2015-08-05');

systate()/current_date() not working in HQL

while doing this operation in HQl i am getting
java sql exception:error 10011 - invalid function sysdate(). getting same for current date too..
WHERE TO_DATE (CONCAT(year,'-',month,'-',day)) >= sysdate()-8
AND TO_DATE (CONCAT(year,'-',month,'-',day)) <= sysdate()-2
how i can rewrite it?
help me out of this
as of hive 1.2.0 you can use current_date() , on previous versions it can be achieved with >=date_sub(from_unixtime(unix_timestamp()),8) f.i.
You should be able to use unix_timestamp() / from_unixtime(unix_timestamp()) (in your case) according to hive manual no matter which version of hive you are using
You can Use the Condition in where clause :
(((unix_timestamp(usage_dt,'yyyy-MM-dd')) >= (unix_timestamp(date_sub(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd') ,8),'yyyy-MM-dd'))) and ((unix_timestamp(usage_dt,'yyyy-MM-dd')) >= (unix_timestamp(date_sub(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd') ,2),'yyyy-MM-dd'))))
Here usage_dt is you column name for date, and the DateFormat In Hive used as "yyyy-MM-dd" format so if you are havig any other format you can mention as here yyyyMMdd is the customr format of the sate.

Function to substract 2 dates in sql with PostgreSQL

I need a way to substract to dates and get the result in days in sql.
I'm using PostgreSQL 8.4
When both columns you want to subtract are of date type, you can just use the - operator.
Take a look at the documentation for date functions, in particular table 9-25.
Check here the documentation of PostgreSQl...
postgresql.org/docs/8.4
I copied it from this code from this website:
age(timestamp '2001-04-10', timestamp '1957-06-13')
hope it will help you...