Need to calculate Age in days of a record

Database
Fan

Need to calculate Age in days of a record

I have been using the following string in a reporting query which pulls data from a server via ODBC.  It is no longer working as expected:

cast (((SYSDATETIME ( ) - a.[Create Date/Time]))as int) as Age

The resulting error is:  The data types datetime2 and datetime are incompatible in the subtract operator.

Any help would be appreciated. 

2 REPLIES
Junior Contributor

Re: Need to calculate Age in days of a record

This is SQL Server syntax, why don't you ask in a Microsoft forum?

SYSDATETIME returns a datetime2 and you can't use plus/minus on that datatype, you need to use DATEDIFF instead:

DATEDIFF(day, a.[Create Date/Time], SYSDATETIME ( ))
Fan

Re: Need to calculate Age in days of a record

I inherited the query from someone else and did not realize I should post elsewhere.  Thanks you so much for your help, Dieter.