1. Get the number of months, weeks, and days between two dates syntax
--1. Get the number of months, weeks, and days between two dates --1.1) Declaring parameters Declare @startDate varchar( -) Declare @endDate varchar( -) --1.2) Get the number of months directly on the two date Select DATEDIFF(MM,@startDate,@endDate) --1.3) Get the number of months directly on the two date Select DATEDIFF(WW,@startDate,@endDate) --1.4) Get the number of months directly on the two date Select DATEDIFF(DD,@startDate,@endDate)
2. Test Cases
--2. Test Cases --2.1) Gets the number of months directly from the specified date and the current date Select DATEDIFF(MM,'2017-08-12 11:12:23',GETDATE()) asnumber of months--2.2) Gets the number of weeks directly from the specified date and the current date Select DATEDIFF(WW,'2017-08-12 11:12:23',GETDATE()) asWeek number--2.3) Gets the number of months directly from the specified date and the current date Select DATEDIFF(DD,'2017-08-12 11:12:23',GETDATE()) asDays
3. Test results
SQL, how to get the number of months, weeks, and days before two dates