Thursday, July 30, 2020

DATEDIFF FUNCTION IN SQL SERVER

DATEDIFF: The DATEDIFF function returns the difference between two date values or returns the time between two dates in sql server.
Now, we will calculate Years,Month,Days,Hours,Minute,Second etc by using DATEDIFF function.

DECLARE @DATE1 DATETIME = GETDATE()
DECLARE @DATE2 DATETIME = '2020-08-15'
SELECT @DATE1 AS DATE1,  @DATE2 AS DATE2

CALCULATE YEAR:
SELECT DATEDIFF(YEAR,@DATE1,@DATE2) AS CALULATED_YEAR

CALCULATE MONTH:
SELECT DATEDIFF(MONTH,@DATE1,@DATE2) AS CALULATED_MONTH

CALCULATE DAY:
SELECT DATEDIFF(DAY,@DATE1,@DATE2) AS CALULATED_DAY

CALCULATE HOUR:
SELECT DATEDIFF(HOUR,@DATE1,@DATE2) AS CALULATED_HOUR 

CALCULATE MINUTE:
SELECT DATEDIFF(MINUTE,@DATE1,@DATE2) AS CALULATED_MINUTE

CALCULATE SECOND:
SELECT DATEDIFF(SECOND,@DATE1,@DATE2) AS CALULATED_SECOND

You can see the below screenshot for further clarifications,



              - - - - - - Cheers, Happy to Help! - - - - - - 

No comments:

Post a Comment

DIFFERENCE BETWEEN CHAR AND VARCHAR IN SQL SERVER

CHAR:  The  CHAR datatype is a fixed length data type in sql server. It is used to store fixed length type of string data or character strin...