In SQL server if you have columns which are of Date/Time type then sometimes you need to find their difference for passing into the application when needed.
So for doing that SQL Server provides a function called DATEDIFF, it is mainly used for finding the Date and Time difference in SQL.
-- Daywise Difference
SELECT DATEDIFF(day, GETDATE(), ExpiryDate) FROM EMPLOYEE
We can get the difference in the terms of days years minutes hours that depends on the query arguments passed.
-- Hours Difference
SELECT DATEDIFF( hour, GETDATE(), ExpiryDate) FROM EMPLOYEE
We can also use this function for setting the values in the SQL statement.
UPDATE NUDGE
SET DaysTillExpire = CASE WHEN DATEDIFF(day, GETDATE(), ExpiryDate) < 0 THEN 0
ELSE DATEDIFF(day, GETDATE(), ExpiryDate)
END
,
HoursTillExpire = CASE WHEN DATEDIFF( hour, GETDATE(), ExpiryDate) < 0 THEN 0
ELSE DATEDIFF(day, GETDATE(), ExpiryDate)
END
,
IsExpired = CASE WHEN DATEDIFF(day, GETDATE(), ExpiryDate) < 0 AND DATEDIFF( hour, GETDATE(), ExpiryDate) < 0 THEN 1
ELSE 0
END
0 Comment(s)