Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
adbasanta
Posting Yak Master
120 Posts |
Posted - 2012-07-09 : 22:22:05
|
Good day!I started to create a temporary table to hold filter dates by month base on @FilterDate and @ToDate value. I currently have this incomplete query and I hope this can be completed.USE MFR_MerchandiseGO DECLARE @FilterDate AS TABLE(filterdate date NOT NULL) DECLARE @FromDate as dateDECLARE @ToDate as dateset @FromDate='11-02-2011' -- can be dynamicset @ToDate='02-24-2012' -- can be dynamicINSERT INTO @FilterDate-- <filter dates base on @FromDate and to @ToDate value must be inserted>-- example: if @FromDate='11-02-2011' and @ToDate='02-24-2012'-- the value to be inserted will be the following dates-- 11-30-2011-- 12-31-2011-- 01-31-2012-- 02-24-2012-- Notice that the date between @FromDate and @ToDate dates are move -- to the end of its month-- EXCEPT THE LAST MONTH which will be the original value in @ToDate. I need this temporary storage table for data crosstab pivoting.Thank you!-------------------------------------------------------------------------------------------------------Learning MS SQL Server 2008 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-09 : 23:19:36
|
[code];With CTE (MonthDate)AS(SELECT DATEADD(mm,DATEDIFF(mm,0,@fromdate)+1,0) -1UNION ALLSELECT CASE WHEN DATEADD(mm,DATEDIFF(mm,0,MonthDate)+1,0)-1 > @ToDate THEN @ToDate ELSE DATEADD(mm,DATEDIFF(mm,0,MonthDate)+1,0)-1 ENDFROM CTEWHERE DATEDIFF(mm,0,MonthDate) < = @Todate)SELECT MonthDateFROM CTEOPTION (MAXRECURSION 0)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-07-09 : 23:33:46
|
the WHERE clause doesn't seems rightquote:
SELECT CASE WHEN DATEADD(mm,DATEDIFF(mm,0,MonthDate)+1,0)-1 > @ToDate THEN @ToDate ELSE DATEADD(mm,DATEDIFF(mm,0,MonthDate)+1,0)-1 ENDFROM CTEWHERE DATEDIFF(mm,0,MonthDate) < = @Todate
KH[spoiler]Time is always against us[/spoiler] |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-07-09 : 23:35:09
|
[code]; with rcte as( select FilterDate = dateadd(month, datediff(month, 0, @FromDate) + 1, -1) union all select FilterDate = @ToDate union all select FilterDate = dateadd(month, datediff(month, 0, FilterDate) + 2, -1) from rcte where dateadd(month, 1, FilterDate) <= @ToDate)select *from rcteoption (maxrecursion 0)[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
adbasanta
Posting Yak Master
120 Posts |
Posted - 2012-07-09 : 23:49:17
|
thank you visakh16 for showing a solution..quote: Originally posted by visakh16
;With CTE (MonthDate)AS(SELECT DATEADD(mm,DATEDIFF(mm,0,@fromdate)+1,0) -1UNION ALLSELECT CASE WHEN DATEADD(mm,DATEDIFF(mm,0,MonthDate)+1,0)-1 > @ToDate THEN @ToDate ELSE DATEADD(mm,DATEDIFF(mm,0,MonthDate)+1,0)-1 ENDFROM CTEWHERE DATEDIFF(mm,0,MonthDate) < = @Todate)SELECT MonthDateFROM CTEOPTION (MAXRECURSION 0) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
-------------------------------------------------------------------------------------------------------Learning MS SQL Server 2008 |
 |
|
adbasanta
Posting Yak Master
120 Posts |
Posted - 2012-07-09 : 23:50:45
|
thank you khtan. Your solution works perfectly.quote: Originally posted by khtan
; with rcte as( select FilterDate = dateadd(month, datediff(month, 0, @FromDate) + 1, -1) union all select FilterDate = @ToDate union all select FilterDate = dateadd(month, datediff(month, 0, FilterDate) + 2, -1) from rcte where dateadd(month, 1, FilterDate) <= @ToDate)select *from rcteoption (maxrecursion 0) KH[spoiler]Time is always against us[/spoiler]
-------------------------------------------------------------------------------------------------------Learning MS SQL Server 2008 |
 |
|
|
|
|
|
|