본문 바로가기
MS-SQL/Query

간단한 달력 만들어보기 예제

by Hwoarang757 2013. 2. 1.

IF EXISTS(SELECT * FROM DBO.SYSOBJECTS WHERE XTYPE = 'U' AND NAME = 'T_CARLENDAR')

BEGIN

                                        DROP TABLE T_CARLENDAR

END

 

CREATE TABLE DBO.T_CARLENDAR

(

TYEAR NVARCHAR(4),

TMONTH NVARCHAR(2),

TDAY NVARCHAR(2),

TDW NVARCHAR(30),

HOLIDAYBOOL BIT DEFAULT 0,

HOLIDAYINFO NVARCHAR(100),

MEMO TEXT

)

 

 

DECLARE @LASTDATETIME DATETIME

SET @LASTDATETIME = '2015-12-31'

 

DECLARE @DATETIME DATETIME

SET @DATETIME  = '2012-11-01'

 

 

WHILE @LASTDATETIME >= @DATETIME 

BEGIN

 

             INSERT T_CARLENDAR

             (

                           TYEAR,TMONTH,TDAY,TDW,HOLIDAYINFO,HOLIDAYBOOL

             )

             SELECT DATENAME(YYYY,@DATETIME) AS TYEAR,

             DATENAME(MM,@DATETIME)  AS TMONTH,

             CASE WHEN LEN(DATENAME(DD,@DATETIME)) = 1 THEN  '0' + DATENAME(DD,@DATETIME) ELSE DATENAME(DD,@DATETIME) END AS TDAY,

             DATENAME(DW,@DATETIME) AS TDW,

             CASE WHEN DATENAME(DW,@DATETIME) IN ('토요일','일요일') THEN '휴무일' ELSE NULL END AS HOLIDAYINFO,

             CASE WHEN DATENAME(DW,@DATETIME) IN ('토요일','일요일') THEN 1 ELSE 0 END AS HOLIDAYBOOL

 

 

                           SET @DATETIME = DATEADD(DAY,1,@DATETIME)

                          

            

END

 

SELECT * FROM T_CARLENDAR