Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

tsql - Subtracting dates in SQL Server

I am trying to convert a PostgreSQL query to a T-SQL query, and I can not get the syntax correct for this PostgreSQL line:

ROUND(CAST((CURRENT_DATE - setupdate) AS decimal) /365,1) AS RetentionYears

When converting it to T-SQL, I have tried this

ROUND(CAST((GETDATE() - setupdate) AS Decimal) / 365, 1) AS RetentionYears

But when I run that I get the error

Msg 402, Level 16, State 1, Line 43
The data types datetime and date are incompatible in the subtract operator.

What would be the proper way to write that statement in T-SQL?

question from:https://stackoverflow.com/questions/65866806/subtracting-dates-in-sql-server

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You want datediff():

DATEDIFF(year, current_timestamp, setupdate) As RetentionYears

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...