Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

How to get week of a mont with input parameter date. Expand / Collapse
Author
Message
Posted Wednesday, September 12, 2012 7:39 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:20 PM
Points: 23,081, Visits: 31,617
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;




Code rewritten for SQL Server 2005:


declare @TestDate date = '2012-09-11';
with SevenRows(n) as (select
row_number() over (order by (select null)) - 1
from (select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1)dt(n))
select dateadd(dd, n,dateadd(wk,datediff(wk,0,dateadd(dd,-1,@TestDate)),0)) from SevenRows;





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1357971
Posted Wednesday, September 12, 2012 4:08 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 3:00 PM
Points: 36,789, Visits: 31,247
xRafo (9/11/2012)
Hi,
i have a question,
How to get First week of a actual month?


Please define what you mean by "first week of the month". For example, is it the actual first 7 days of the month regardless of which day of the week it starts on? Is it the week starting on Sunday (for example) that contains the first of the month even if it starts on a Saturday? Is it the week of the month starting on Sunday (for example) that contains at least the first 4 calendar days of the month (like ISO).

What would you define as the first week of September 2012 and why (for example???)

Now... all of that appears to be contrary to the rest of your request...

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/09

any help would be useful.


If that's what you really want, then the other posters have already posted some dandy ideas on how to do that.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1358248
Posted Wednesday, September 12, 2012 8:25 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 3:00 PM
Points: 36,789, Visits: 31,247
Lynn Pettis (9/12/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;




Code rewritten for SQL Server 2005:


declare @TestDate date = '2012-09-11';
with SevenRows(n) as (select
row_number() over (order by (select null)) - 1
from (select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1)dt(n))
select dateadd(dd, n,dateadd(wk,datediff(wk,0,dateadd(dd,-1,@TestDate)),0)) from SevenRows;




I have a general distrust for the "wk" datepart even though it doesn't seem to matter matter here. With that thought in mind, here's a bit of code that uses a zero based Tally Table. It can be easily modified to handle a unit based Tally Table if needed. If nothing else, it makes for some really simple code.

  SELECT DATEADD(dd,DATEDIFF(dd,-1,GETDATE())/7*7+(t.N),-1)
FROM dbo.Tally t
WHERE t.N BETWEEN 0 AND 6



--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1358298
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse