[FIXED] Microsoft SQL Server 2016 Date as Numerical Representation

Issue

Does anyone know if there is a way to get a number representation of a date, the same way DB2 does theirs?

rel="nofollow noreferrer">https://www.ibm.com/docs/pl/db2-for-zos/11?topic=functions-days

SELECT 
POSTING_DATE,
DAYS(POSTING_DATE) AS NUMBER_REPRESENTATIOn
FROM core.env

enter image description here

I've been searching but, cannot seem to find a solution. Thanks!


Solution

The documentation seemed pretty clear, you just have to translate the formula to use T-SQL syntax (where we measure the number of days between two dates using DATEDIFF):

DECLARE @d date = '20220808';

SELECT DATEDIFF(DAY, '00010101', @d) + 1;

Result:

738,375



Answered By - Aaron Bertrand
Answer Checked By - Pedro (sql.easybugfix Volunteer)

Related Posts

Post a Comment