Subtracting Datetime in SQL query

2019-08-24 11:39发布

问题:

I have a Sql view Im trying to write and I'd like to be able to take my DateTime Field (called OrderDate) and then subtract it from the current Time (GetDate()) to show me how long its been in days since the OrderDate. Can someone help out?

Thanks

回答1:

I am assuming you are using SQL Server, since you mention the T-SQL GETDATE() function.

For calculations on differences between two date, use the DATEDIFF function:

SELECT DATEDIFF(day, OrderDate, GETDATE())
FROM myTable