Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Operand data type date is invalid for subtract operator.

$
0
0

Hi,

I need to write  a function that returns the age of the users in years. I need precise age, so a simple datediff(yy, ..) is not suitable.

The problem is that the database has unrealistic data as well and business wants to see it too. So the function has to work for very old users as well ones who will be born in the future :S.

I have the following script, and I don't really understand why I receive the error message described in the subject.

declare @dob date
set @dob = '9998-02-11'

select
case when @dob <= CAST(GETDATE() as date) then datepart(yy, CAST(GETDATE() as date)-@dob)-datepart(yy, 0)
else -1*(datepart(yy, @dob - CAST(GETDATE() as date))-datepart(yy, 0)) end

Any help is appreciated.

Thanks.

P.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>