Bonjour à tous ,
Je souhaite créer une table calendrier pour deux année ex: 2012 et 2013 qui contiendra l'année , date du début de semaine ,date de fin de semaine et le numéro de semaine.
Avez vous une idée sur le script ?
Merci
Bonjour à tous ,
Je souhaite créer une table calendrier pour deux année ex: 2012 et 2013 qui contiendra l'année , date du début de semaine ,date de fin de semaine et le numéro de semaine.
Avez vous une idée sur le script ?
Merci
Voici un exemple de table calendrier que j'ai trouvé sur google :
Tu n'as plus qu'à ajouter les colonnes manquantes...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 CREATE TABLE [dbo].calendrier( [id] [int] IDENTITY(1,1) NOT NULL, [fulldate] [date] NOT NULL, [annee] AS (datepart(year,[fulldate])) PERSISTED, [semestre] AS ((CONVERT([char](4),datepart(year,[fulldate]))+'_')+case when datepart(month,[fulldate])<(7) then '1' else '2' end) PERSISTED, [trimestre] AS ((CONVERT([char](4),datepart(year,[fulldate]))+'_')+case when datepart(month,[fulldate])<(4) then '1' else case when datepart(month,[fulldate])<(7) then '2' else case when datepart(month,[fulldate])<(10) then '3' else '4' end end end) PERSISTED, [mois] AS ((CONVERT([char](4),datepart(year,[fulldate]))+'_')+case when len(CONVERT([varchar](2),datepart(month,[fulldate])))=(1) then '0'+CONVERT([varchar](2),datepart(month,[fulldate])) else CONVERT([varchar](2),datepart(month,[fulldate])) end) PERSISTED, [semaine] AS ((CONVERT([char](4),datepart(year,[fulldate]))+'_')+case when len(CONVERT([varchar](2),datepart(week,[fulldate])))=(1) then '0'+CONVERT([varchar](2),datepart(week,[fulldate])) else CONVERT([varchar](2),datepart(week,[fulldate])) end), [jour] AS ((((CONVERT([char](4),datepart(year,[fulldate]))+'_')+case when len(CONVERT([varchar](2),datepart(month,[fulldate])))=(1) then '0'+CONVERT([varchar](2),datepart(month,[fulldate])) else CONVERT([varchar](2),datepart(month,[fulldate])) end)+'_')+case when len(CONVERT([varchar](2),datepart(day,[fulldate])))=(1) then '0'+CONVERT([varchar](2),datepart(day,[fulldate])) else CONVERT([varchar](2),datepart(day,[fulldate])) end) PERSISTED, CONSTRAINT [PK_calendrier] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO DECLARE @DATEDEBUT DATETIME DECLARE @DATEFIN DATETIME SET @DATEDEBUT = '20120101' SET @DATEFIN = '20131231' ;with mycte as ( select @DATEDEBUT DateValue union all select DateValue + 1 from mycte where DateValue + 1 < @DATEFIN ) INSERT INTO calendrier([fulldate]) select CAST(DateValue as DATE) from mycte OPTION (MAXRECURSION 0)
Avec le code de remplissage
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 CREATE TABLE [dbo].[Calendrier]( [Date] [date] NOT NULL, [Mois] [int] NULL, [Annee] [int] NULL, [Semaine] [int] NULL, [JourSemaine] [int] NULL, [DateDimanche] [date] NULL, CONSTRAINT [PK_Calendrier] PRIMARY KEY CLUSTERED ( [Date] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET DATEFIRST 1 GO Declare @Date Date = (select max(Date) from Calendrier ) IF @Date IS NULL BEGIN SET @Date = '19741231' END SET @Date = dateadd(day,1,@date) insert into Calendrier SELECT @Date Date, DATEPART(month,@date) Mois, DATEPART(Year,@date) Annee,dbo.fNoSemaine_Groupe(@Date) Semaine,DATEPART(weekday,@Date)-Round(@@DATEFIRST/7,0) JourSemaine,Dbo.GET_DATE_DIM(@Date) DateDimanche GO 30000
Merci pour ta réponse ton code n'est pas mal mais je dois cela dit essayer de comprendre les fonction date.
Cela dit je ne sais pas comment je pourrais récupérer la date qui correspond au début et fin de semaine !!!
T'as tapé quoi sur google ? j'ai fait pas mal de recherches et je n'ai pas vraiment trouvé de bons exemples .
Consulte le blog d'Elsuket pour y trouver tes réponses.
ou encore : http://sqlpro.developpez.com/cours/gestiontemps/
A +
Frédéric Brouard - SQLpro - ARCHITECTE DE DONNÉES - expert SGBDR et langage SQL
Le site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
Blog SQL, SQL Server, SGBDR : http://blog.developpez.com/sqlpro
Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
Entreprise SQL SPOT : modélisation, conseils, audit, optimisation, formation...
* * * * * Expertise SQL Server : http://mssqlserver.fr/ * * * * *
« Je ne cherche pas à connaître les réponses, je cherche à comprendre les questions. »
- Confucius -
Les meilleurs cours, tutoriels et Docs sur les SGBD et le SQL
Tous les cours Office
Solutions d'Entreprise
![]()
Partager