To Find month difference between two dates, we must use the datediff function. With DateDifference function we can get all kinds of date types. The datediff function gets three parameters.
First Parameter: Date interval type. For month, we must write “MM” or “MONTH” statements
Second Parameter: Small Date
Third Parameter: Older Date
Example Queries
EX 1) Calculate the day count between ‘09.05.2018’ and ‘10.05.2018’ dates;
1 2 3 |
Select DATEDIFF(MONTH,'09.05.2018','10.05.2018') OR Select DATEDIFF(MM,'09.05.2018','10.05.2018') |
Result: 1
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: 0-> Because in Turkish dateformat is day/month/Year in English Date Format is Month/Day/Year
EX 2) Calculate the month count between ‘09.05.2018’ and today; (English Format)
1 2 3 |
Select DATEDIFF(MONTH,'09.05.2018',getDate()) OR Select DATEDIFF(MM,'09.05.2018',getDate()) |
Result: 6 ->This Date: March 23,2019
EX3) Calculate the month count between student’s birthdate and today;
1 |
Select Students.*,DATEDIFF(MONTH,birthdate,getdate()) as MonthCount from students |