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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 storing dynamic dates on temp table..?

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_Merchandise
GO

DECLARE @FilterDate AS TABLE(filterdate date NOT NULL)
DECLARE @FromDate as date
DECLARE @ToDate as date

set @FromDate='11-02-2011' -- can be dynamic
set @ToDate='02-24-2012' -- can be dynamic

INSERT 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) -1

UNION ALL

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 END
FROM CTE
WHERE DATEDIFF(mm,0,MonthDate) < = @Todate
)

SELECT MonthDate
FROM CTE

OPTION (MAXRECURSION 0)
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-07-09 : 23:33:46
the WHERE clause doesn't seems right
quote:

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 END
FROM CTE
WHERE DATEDIFF(mm,0,MonthDate) < = @Todate




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

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 rcte
option (maxrecursion 0)[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

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) -1

UNION ALL

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 END
FROM CTE
WHERE DATEDIFF(mm,0,MonthDate) < = @Todate
)

SELECT MonthDate
FROM CTE

OPTION (MAXRECURSION 0)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





-------------------------------------------------------------------------------------------------------
Learning MS SQL Server 2008
Go to Top of Page

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 rcte
option (maxrecursion 0)



KH
[spoiler]Time is always against us[/spoiler]





-------------------------------------------------------------------------------------------------------
Learning MS SQL Server 2008
Go to Top of Page
   

- Advertisement -