## How to get week of a mont with input parameter date.

 Author Message xRafo SSC-Enthusiastic Group: General Forum Members Points: 123 Visits: 419 Hi,i have a question,How to get First week of a actual month?In my procedure i will send a parameter date: Example input -> today()output <- 10/09 | 11/09 | 12/09 | 13/09 | 14/09 | 15/09 | 16/09any help would be useful.Thanks.Pd. Sorry for my bad engl. ____________________________________________________________________________Rafo* GSquared SSC Guru Group: General Forum Members Points: 94695 Visits: 9730 Easy enough:`DECLARE @InputDate DATE = GETDATE();SELECT DATEADD(month, DATEDIFF(MONTH, 0, @InputDate), 0) AS FirstDayOfMonth, DATEADD(week, DATEDIFF(week, 0, DATEADD(month, DATEDIFF(MONTH, 0, @InputDate), 0)), 0) - 1 AS FirstDayOfWeek, DATEADD(week, DATEDIFF(week, 0, DATEADD(month, DATEDIFF(MONTH, 0, @InputDate), 0)), 0) AS SecondDayOfWeek, DATEADD(week, DATEDIFF(week, 0, DATEADD(month, DATEDIFF(MONTH, 0, @InputDate), 0)), 0) + 1 AS ThirdDayOfWeek, DATEADD(week, DATEDIFF(week, 0, DATEADD(month, DATEDIFF(MONTH, 0, @InputDate), 0)), 0) + 2 AS FourthDayOfWeek, DATEADD(week, DATEDIFF(week, 0, DATEADD(month, DATEDIFF(MONTH, 0, @InputDate), 0)), 0) + 3 AS FifthDayOfWeek, DATEADD(week, DATEDIFF(week, 0, DATEADD(month, DATEDIFF(MONTH, 0, @InputDate), 0)), 0) + 4 AS SixthDayOfWeek, DATEADD(week, DATEDIFF(week, 0, DATEADD(month, DATEDIFF(MONTH, 0, @InputDate), 0)), 0) + 5 AS SeventhDayOfWeek;`The nested DateAdd, DateDiff method allows you to get the first X (time-unit) of any given DateTime value. If you use Days as your unit, you get the very beginning of the day (midnight at the end of the prior day, to be precise). If you use Weeks as your unit, you get the first day of the week. And so on.This one has to be done in two stages:First, get the first day of the month. I put that in its own column, just so you could see how it's done. You can remove that column from the query if you don't want it.Second, get the first day of the week that the first day of the month is in. So it nests the month calculation inside a week calculation. The math at the end of each row is based on Monday being the first day of the week on the server I ran this on. You'll need to confirm that and may need to change the "-1", "+1" through "+5" if the first day of the week is defined as Sunday or whatever on your server. - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETCProperty of The Thread"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon Lynn Pettis SSC Guru Group: General Forum Members Points: 150598 Visits: 39285 Here is a bit of code that returns what you are looking for:`declare @TestDate date = '2012-09-11';with SevenRows(n) as (select row_number() over (order by (select null)) - 1 from (values (1),(1),(1),(1),(1),(1),(1))dt(n))select dateadd(dd, n,dateadd(wk,datediff(wk,0,dateadd(dd,-1,@TestDate)),0)) from SevenRows;` Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) GSquared SSC Guru Group: General Forum Members Points: 94695 Visits: 9730 Lynn Pettis (9/11/2012)Here is a bit of code that returns what you are looking for:`declare @TestDate date = '2012-09-11';with SevenRows(n) as (select row_number() over (order by (select null)) - 1 from (values (1),(1),(1),(1),(1),(1),(1))dt(n))select dateadd(dd, n,dateadd(wk,datediff(wk,0,dateadd(dd,-1,@TestDate)),0)) from SevenRows;`It's the SQL 2005 forum, Lynn. He might not have access to TVFs. - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETCProperty of The Thread"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon Lynn Pettis SSC Guru Group: General Forum Members Points: 150598 Visits: 39285 GSquared (9/11/2012)Lynn Pettis (9/11/2012)Here is a bit of code that returns what you are looking for:`declare @TestDate date = '2012-09-11';with SevenRows(n) as (select row_number() over (order by (select null)) - 1 from (values (1),(1),(1),(1),(1),(1),(1))dt(n))select dateadd(dd, n,dateadd(wk,datediff(wk,0,dateadd(dd,-1,@TestDate)),0)) from SevenRows;`It's the SQL 2005 forum, Lynn. He might not have access to TVFs.SQL Server 2005 has table valued functions, I'm lost. Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) Lynn Pettis SSC Guru Group: General Forum Members Points: 150598 Visits: 39285 Plus, we only have part of the problem. We should wait for the other shoe to drop and see how this is going to be used in the procedure. Also, he did say a parameter was being passed, the single input value, and wanted 7 values returned. Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) Phil Parkin SSC Guru Group: General Forum Members Points: 86273 Visits: 21705 Lynn Pettis (9/11/2012)Plus, we only have part of the problem. We should wait for the other shoe to drop and see how this is going to be used in the procedure. Also, he did say a parameter was being passed, the single input value, and wanted 7 values returned.Also puzzling (to me at least) is the "First week of a actual month" requirement - whereas the sample data supplied was not the first week in September, by my reckoning. Surely it would be the week commencing 2 or 3 September (depending on which day you choose as the start day)? Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot. GSquared SSC Guru Group: General Forum Members Points: 94695 Visits: 9730 Lynn Pettis (9/11/2012)GSquared (9/11/2012)Lynn Pettis (9/11/2012)Here is a bit of code that returns what you are looking for:`declare @TestDate date = '2012-09-11';with SevenRows(n) as (select row_number() over (order by (select null)) - 1 from (values (1),(1),(1),(1),(1),(1),(1))dt(n))select dateadd(dd, n,dateadd(wk,datediff(wk,0,dateadd(dd,-1,@TestDate)),0)) from SevenRows;`It's the SQL 2005 forum, Lynn. He might not have access to TVFs.SQL Server 2005 has table valued functions, I'm lost.No, it doesn't. I just tried it on SQL 2005 Dev Edition, and it didn't work. Plus, per TechNet, it was a new feature in SQL 2008 (ref: http://technet.microsoft.com/en-us/library/cc721270(v=SQL.100).aspx). - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETCProperty of The Thread"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon Luis Cazares SSC Guru Group: General Forum Members Points: 65371 Visits: 20250 In 2005 there are TVF (Table Valued Functions)There are not Table Value Constructors.Is that what you meant?It can be fixed by`declare @TestDate date = '2012-09-12';with SevenRows(n) as ( select row_number() over (order by (select null)) - 1 from (SELECT TOP 7 NULL FROM sys.columns)dt(n))select dateadd(dd, n,dateadd(wk,datediff(wk,0,dateadd(dd,-1,@TestDate)),0)) from SevenRows;` Luis C.General Disclaimer:Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?How to post data/code on a forum to get the best help: Option 1 / Option 2 Lynn Pettis SSC Guru Group: General Forum Members Points: 150598 Visits: 39285 Luis Cazares (9/12/2012)In 2005 there are TVF (Table Valued Functions)There are not Table Value Constructors.Is that what you meant?It can be fixed by`declare @TestDate date = '2012-09-12';with SevenRows(n) as ( select row_number() over (order by (select null)) - 1 from (SELECT TOP 7 NULL FROM sys.columns)dt(n))select dateadd(dd, n,dateadd(wk,datediff(wk,0,dateadd(dd,-1,@TestDate)),0)) from SevenRows;`Now that I will agree with and can correct.Thanks, Luis. Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)