Reputation: 143
I want to calculate weeks and days between 2 Dates, Like that IN SQL QUERY
if Date from : 10-01-2018 Date to : 19-01-2018
so i want the result "1 week and 2 days"
Upvotes: 0
Views: 427
Reputation: 465
Try this.
DECLARE @From datetime
DECLARE @To datetime
SET @From ='20180110'
SET @To ='20180119'
SELECT DATEDIFF(ww,@From, @To) AS Week,
DATEDIFF(dd,@From, @To)%7 AS Days
Upvotes: 1
Reputation: 444
If you are using MSSQL (Transact-SQL) you can use the datediff-method:
DATEDIFF ( datepart , startdate , enddate )
whic in your case would be
DATEDIFF(day,10-01-2018, 19-01-2018);
And then divide the days with 7 and take the remainer as days.
More info at MS Docs
Upvotes: 1
Reputation: 6018
Use the DATEDIFF
function with datepart=day
.
The number of weeks is the result divided by 7 and the number of days is the remainder
Upvotes: 0