I have a week number, like 32, and I need get the number of days left in the month starting from this week. How can I do in SQL Server?
Asked
Active
Viewed 145 times
0
-
2`@number_of_days = 7`? – GSerg Aug 09 '17 at 08:39
-
1I need to get the month of this weekNumber. Then i need to know the for Week number 32 remaining days of the respective month. – user3510330 Aug 09 '17 at 08:41
-
1Possible duplicate of [Get dates from a week number in T-SQL](https://stackoverflow.com/questions/607817/get-dates-from-a-week-number-in-t-sql) – GSerg Aug 09 '17 at 08:44
-
you mean you need to get 7 days for that week? – Kannan Kandasamy Aug 09 '17 at 08:44
-
1You should update your question, with what you really need, and some example output. Unless a specific week only has 1 day, the days left in the month is non-deterministic. – Peter Abolins Aug 09 '17 at 08:46
2 Answers
0
Try DATEPART ( datepart , date )
and datepart
can be weekday, day, month or something like that..
Visit this https://technet.microsoft.com/en-us/library/ms174420(v=sql.105).aspx

flowers1234
- 339
- 2
- 9
- 21
0
First, you need to find the first day of the week number in question. In order to do this, we need to know what year you want as well. In the example below, I use the current year. Once this is done, then we can use DATEDIFF()
to find the days left, from the start of the week number you passed in, to the end of the month.
DECLARE @WeekNo int= 32
DECLARE @Year int=datepart(year,getdate())
SELECT
FirstDayOfWeek = DATEADD(wk,@WeekNo-1,DATEADD(yy,@Year-1900,0))
,LastDayOfMonth = DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, DATEADD(wk,@WeekNo-1,DATEADD(yy,@Year-1900,0))) + 1, 0))
,DaysLeftInMonth = DATEDIFF(day,DATEADD(wk,@WeekNo-1,DATEADD(yy,@Year-1900,0)),DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, DATEADD(wk,@WeekNo-1,DATEADD(yy,@Year-1900,0))) + 1, 0)))

S3S
- 24,809
- 5
- 26
- 45