How to Use the DATEDIFF() Function in SQL Server: A Detailed Guide

Rumman Ansari   Software Engineer   2024-07-21 09:49:45   5785  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Syntax:


DATEDIFF(datepart, startdate, enddate)

DATEDIFF(datepart, startdate, enddate) - Returns the count of the specified datepart boundaries crossed between the specified startdate and enddate.

DatePart Abbreviations
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw
hour hh
minute mi, n
second ss, s
millisecond ms
microsecond mcs
nanosecond ns
TZoffset tz
ISO_WEEK isowk, isoww

Code:


Select DATEDIFF(MONTH, '11/30/2005','01/31/2006') 
 
Select DATEDIFF(DAY, '11/30/2005','01/31/2006')  

Output:

The above code will produce the following result-


2
62



Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.