Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Selecting dates not in a date set
Message
From
19/07/2007 16:21:56
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01241975
Message ID:
01242135
Views:
16
Hi, it's very easy to create a range of dates using CTEs...

Here's an example....suppose I want to create a CTE of Saturday dates from 1-6-07 to 7-7-07...
DECLARE @StartDate DATETIME, @EndDate DATETIME
SET @StartDate = '1-6-2007'   -- you might want to verify this is a saturday date
SET @EndDate = '7-7-2007'   

;WITH DateCTE(WeekEndingCTE)  AS
        -- anchor query
        (SELECT @StartDate AS WeekEnding   
                   UNION ALL 
                       -- recursive query
                       SELECT WeekEnding + 7 AS WeekEnding FROM WeekEndingCTE WHERE WeekEnding < @EndDate )

-- Now do something wtih WeekEndingCTE
That will give you a list of every Saturday date in that date range.

(now, in JB's example, you'd add 1 day instead of 7, if you need every date in the range)

Let me know if that helps...
Kevin
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform