Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How To - Create Table Variable of dates MWF 2 years
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2000
Divers
Thread ID:
01439385
Message ID:
01439396
Vues:
50
This should get you started
DECLARE @StartDate datetime, @Days int
SET @StartDate = '12/16/2009'
SET @Days = 365*2

DECLARE @TableOfDates table
(
WorkDate datetime,
WorkDateNumber int
)
/*
INSERT INTO @TableOfDates
	SELECT (@StartDate+ number) AS dt, DATEPART(dw,@StartDate+ number) AS dow 
		FROM master..spt_values  
		WHERE type = 'P' AND number <= @Days
			AND DATEPART(dw,@StartDate+ number) IN (2,4,6)
*/

INSERT INTO @TableOfDates
	SELECT dt, DATEPART(dw, dt) AS dow 
		FROM (SELECT (@StartDate+ number) AS dt FROM master..spt_values WHERE type = 'P' AND number <= @Days) dates
		WHERE DATEPART(dw, dt) IN (2,4,6)


SELECT * FROM @TableOfDates				
The master..spt_values table is used as a numbers table with values in range 0-2047.

>
>I was wondering if anyone had a quick set of SQL code to create a table variable and populate with two years of dates based off MWD or Tue/Thur/Sat
>
>I will have start date and the string of valid dow. I only need to populate the Workdate and DOW in the table. Other fields will be blank until later processing. I'm not a SQL date wizard (or any wizard) so I didn't know if there is an easy way to figure all the dates for an insert into @TableOfDates.
>
>Thanks for any help.
>
>
>declare @StartDate varchar(10) = '12/16/2009'
>declare @DowsOfWeek = '2,4,6'    -- MWF Example
>declare @TableOfDates table
>(
>WorkDate varchar(10),
>WorkDateNumber int,
>SchedStart varchar(10),
>SchedEnd   varchar(10),
>VarianceFromPlanned int,
>Booked bit
>)
>-- 2,4,6 is MWF
>-- 3,5,7 is TTS
>---------------------------------------------------------------------------------------------
>WorkDate    WorkDateNumber       SchedStart                SchedEnd          VarianceFromPlanned  Booked
>----------------------------------------------------------------------------------------------
>12/16/2009        4  
>121/8/2009        6
>12/21/2009        2                           
>12/23/2009        4
>
--sb--
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform