To Find date difference between two dates, we must use the datediff function. The datediff function gets three parameters.
First Parameter: Date interval type. For day, we must write DD or day statement
Second Parameter: Small Date
Third Parameter: Older Date
Examples
EX 1) Calculate the day count between ‘09.05.2018’ and ‘10.05.2018’ dates;
1 2 3 |
Select DATEDIFF(DAY,'09.05.2018','10.05.2018') OR Select DATEDIFF(DD,'09.05.2018','10.05.2018') |
Result : 30
Important Note: The result changes by the language of the sql server. To change sql server’s language type the code below;
1 |
Set Language Turkish |
New Result: 1 —Because in Turkish dateformat is day/month/Year in English Date Format is Month/Day/Year
EX 2) Calculate the day count between ‘09.05.2018’ and today; (English Format)
1 2 3 |
Select DATEDIFF(DAY,'09.05.2018',getDate()) OR Select DATEDIFF(DD,'09.05.2018',getDate()) |
The Result: 191 (Date:’30.15.2019′);
EX3) Calculate the day count between student’s birthdate and today;
1 |
Select Students.*,DATEDIFF(Day,birthdate,getdate()) as DayCount from students |
Result:
EX4) Calculate the day count between takendate and broughtDate on borrows table,
1 |
Select borrows.*,DATEDIFF(DD,takenDate,broughtDate) as DayCount from borrows |
Hi, Just a slight correction
You state above
“New Result: 1 —Because in Turkish dateformat is day/month/Year in English Date Format is Month/Day/Year”
In fact, day/month/year is the common format in Europe, and is definitely the preferred format in the UK (AKA as “England”). The month/day/year format is the format favoured in North America
Best regards
Thank you for the correction