Date difference in Amazon Aurora postgres and hsql - hsqldb

I am looking for some common approach for taking date difference for hsql and aurora postgres. I know that direct date difference is supported for aurora postgres i.e date1 - date2 > 2 works for it but this is not supported in hsql. DATEDIFF('day' , TO_CHAR(sysdate,'yyyy-MM-dd'),TO_CHAR(mysample_date,'yyyy-MM-dd')) is supported in hsql but not in aurora postgres.
Is there any common approach for the same?
Any lead will be appreciated.

You probably need to use a more recent version of HSQLDB. The latest versions treat the non-standard date1 - date2 > 5 as date1 - data2 > 5 DAY (Standard syntax), while some earlier versions didn't treat it as a DAY difference.

Related

GreenPlum SQL to HiveSQL migration of query statement

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)

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: How to convert future date to timestamp with UNIX_TIMESTAMP()?

I want to convert a DATETIME from a column to a UNIX TIMESTAMP. But the thing is that those dates are in a distant future, such as 2066-09-01... You can try those simple queries:
SELECT UNIX_TIMESTAMP( '2016-09-03 09:00:00' ) returns 1472886000 -> GOOD
SELECT UNIX_TIMESTAMP( '2036-09-03 09:00:00' ) returns 2104038000 -> GOOD
SELECT UNIX_TIMESTAMP( '2066-09-03 09:00:00' ) returns 0 -> BAD! WHY??
Any idea? Any workaround ?
From Unixtimestamp.com:
What happens on January 19, 2038?
On this date the Unix Time Stamp will cease to work due to a 32-bit overflow. Before this moment millions of applications will need to either adopt a new convention for time stamps or be migrated to 64-bit systems which will buy the time stamp a "bit" more time.
Sounds like your rdbms is suffering from the Y2038 problem: https://en.m.wikipedia.org/wiki/Year_2038_problem
Is this MySQL? They have a bug about this: https://dev.mysql.com/worklog/task/?id=1872
Suggest handling this outside SQL if possible. PHP's strtotime() and similar functions in other languages can play nice with MySQL's ISO date format.

Select most recent records by DB2 date in form of YYYYMMDD

I am importing records from a DB2 data source into a MS SQL Server destination.
The records are coming in with the date format of 20150302/YYYYMMDD, but I only want the last 14 days based on current server date.
Can some advise on how to select based on this date format against DATEADD(d, - 1, { fn CURDATE() }) please.
Thanks!
It would be better to do this on the DB2 side, to reduce the number of records brought over.
Additionally, it's better from a performance standpoint to convert the static date into a numeric date and compare to the column in your table. Rather than convert the numeric date in your table to a actual date type for comparison.
where numdate >= int(replace(char(current_date - 14 days,iso),'-',''))
Doing it this way will allow you to take advantage of an index over numdate. In addition, DB2 will only need to perform this conversion once.
Depending on your platform & version, you may have an easier way to convert from a date data type to a numeric date. But the above works on DB2 for i and should work on most (all?) DB2 versions and platforms.
You may find it worthwhile to create a UDF to do this conversion for you.
If you want logic in SQL Server, then you are in luck, because you can just convert the YYYYMMDD format to a date:
where cast(datecol as date) >= cast(getdate() - 14 as date)
(This assumes no future dates.)
If you want to do this on the DB2 side, you can use to_date():
where to_date(datecol, 'YYYYMMDD') >= current date - 14 days