SQL DATEDIFF

SQL Server DATEDIFF Function will find or Calculate the difference between the Start Date and the End Date. The SQL datediff function always returns a signed integer value. For example, If you want to extract the year, month, or quarter from the existing Date, you can use this SQL Server Datediff function.

SQL DATEDIFF Function Syntax

DATEDIFF(Datepart, Start Date, End Date)

Datepart: This is part of a given date that we use to calculate the difference between the Start Date and End Date. The list of available datepart arguments in SQL Server for this DATEDIFF.

DatepartAbbreviationsDescription
yearyy,yyyyDisplay the difference between the Start and End date in terms of Years
quarterqq, qThis Datepart displays the difference between the Start and End date in terms of Quarters
monthmm, mDisplay the difference between the Start and End date in terms of Months
dayofyeardy, yDisplay the difference between the Start and End date in terms of Days of a Year (1 to 365)
daydd, dThis Datepart in Sql Server Datediff display the difference between the Start and End date in terms of Day (1 to 31)
weekwk, wwDisplay the difference between Start and End in terms of Week
weekdaydw, wDisplay the difference between Start and End in terms of Weekday
hourhhThis Datepart in it displays the difference between Start and End in terms of Hours
minutemi, nDisplay the difference between the Start and End in terms of Minute
secondss, sIt displays the difference between Start and End in terms of Seconds
millisecondmsDisplay the difference between the Start date and End in terms of Milliseconds
microsecondmcsThis SQL Server Datediff argument displays the difference between the Start and End date in terms of Microsecond
nanosecondnsIt will print the difference between Start and End in terms of Nanoseconds.

Start: Please specify the valid date as the second argument (Starting Date). It can be a column, expression, or any SQL Server variable.

End: Please specify the valid date as the third argument (Ending Date). It can be a column, an expression, or any variable.

SQL DATEDIFF Function Example

In this SQL Datediff example, we are going to declare two variables of the datetime2 data type. Let us assign a valid date to both variables and perform all the available operations.

First, We declared two variables, @ab and @cd, and assigned different dates and times to those variables.

Within the first statement (year, @ab, @cd), the SQL Datediff function prints the total number of years between the @ab variable and @cd.

Next, we asked the function to display the total number of months between the @ab and @cd variables.

SQL DATEDIFF 1

DATEDIFF Example 2

In this example, we use the custom table to perform SQL Datediff operations on the Hire column and the built-in GETDATE() function. The first statement in the 5th line finds the difference between the Hire Column and today in terms of Year.

In the next line, this SQL DATEDIFF Function will find the difference between the Hire Column and today regarding Quarters. Similarly, you can select milliseconds, hours, minutes, the number of days, and days hours.

SQL DATEDIFF on Table Column
Categories SQL