Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Selecting dates not in a date set
Message
De
19/07/2007 16:21:56
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01241975
Message ID:
01242135
Vues:
17
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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform