Calculation the difference between 2 dates in SQL Server and DB2 -
Calculation the difference between 2 dates in SQL Server and DB2 -
i know there lot ways calculate difference between dates in different databases. problem need way that work in both sql server , db2 using same sql query.
on yearly basis can use
current_timestamp - [birthdate] agethisyear
but want know persons age in decimal form i.e. 34,4 depending on persons birthday , current date.
in sql server columntype [birthdate]
datetime
, in db2 column type date
.
the format yyyy-mm-dd
is there way this?
regards, alfred
i think way can write function work in both databases utilize year()
, month()
, , day()
, or write functions yourself. there not much overlap in date functions between 2 databases.
for instance, total years between 2 dates, use:
select (year(current_timestamp) - year(birthdate) + (case when month(current_timestamp) * 100 + day(current_timestamp) < month(birthdate) * 100 + day(birthdate) -1 else 0 end) ) ageinyears
fractional years much more complicated. of course, each database has own more reasonable methods getting want. methods different.
sql sql-server db2 date-difference
Comments
Post a Comment