SQL_考勤

USE [Tmp]
GO
CREATE TABLE [dbo].[HolidayName](
   
[ID] [tinyint] NOT NULL,
   
[Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
CONSTRAINT [PK_HolidayName] PRIMARY KEY CLUSTERED
(
   
[ID] ASC
)
WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
)
ON [PRIMARY]
GO
INSERT INTO [HolidayName]
           (
[ID]
           ,
[Name])
    
VALUES
           (
1
           ,N
'新年')

INSERT INTO [HolidayName]
           (
[ID]
           ,
[Name])
    
VALUES
           (
2
           ,N
'春节')

INSERT INTO [HolidayName]
           (
[ID]
           ,
[Name])
    
VALUES
           (
3
           ,N
'劳动节')

INSERT INTO [HolidayName]
           (
[ID]
           ,
[Name])
    
VALUES
           (
4
           ,N
'国庆节')

INSERT INTO [HolidayName]
           (
[ID]
           ,
[Name])
    
VALUES
           (
5
           ,N
'妇女节')

INSERT INTO [HolidayName]
           (
[ID]
           ,
[Name])
    
VALUES
           (
6
           ,N
'青年节')

INSERT INTO [HolidayName]
           (
[ID]
           ,
[Name])
    
VALUES
           (
7
           ,N
'儿童节')

INSERT INTO [HolidayName]
           (
[ID]
           ,
[Name])
    
VALUES
           (
8
           ,N
'建军日')

INSERT INTO [HolidayName]
           (
[ID]
           ,
[Name])
    
VALUES
           (
9
           ,N
'清明节')

INSERT INTO [HolidayName]
           (
[ID]
           ,
[Name])
    
VALUES
           (
10
           ,N
'端午节')

INSERT INTO [HolidayName]
           (
[ID]
           ,
[Name])
    
VALUES
           (
11
           ,N
'中秋节')
USE [Tmp]
GO
CREATE TABLE [dbo].[Holiday](
   
[ID] [int] IDENTITY(1,1) NOT NULL,
   
[NameID] [tinyint] NOT NULL,
   
[Date] [char](4) COLLATE Chinese_PRC_CI_AS NOT NULL,
   
[Days] [decimal](3, 1) NOT NULL,
   
[IsPart] [bit] NOT NULL,
   
[Offset] [smallint] NOT NULL,
   
[Last] [varchar](8) COLLATE Chinese_PRC_CI_AS NOT NULL,
   
[First] [varchar](8) COLLATE Chinese_PRC_CI_AS NOT NULL,
CONSTRAINT [PK_Holiday] PRIMARY KEY CLUSTERED
(
   
[NameID] ASC,
   
[Last] ASC
)
WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
)
ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Holiday]  WITH CHECK ADD  CONSTRAINT [FK_Holiday_HolidayName] FOREIGN KEY([NameID])
REFERENCES [dbo].[HolidayName] ([ID])
GO
ALTER TABLE [dbo].[Holiday] CHECK CONSTRAINT [FK_Holiday_HolidayName]
GO
ALTER TABLE [dbo].[Holiday]  WITH CHECK ADD  CONSTRAINT [CK_Holiday_First] CHECK  ((isdate([First])=(1)))
GO
ALTER TABLE [dbo].[Holiday] CHECK CONSTRAINT [CK_Holiday_First]
GO
ALTER TABLE [dbo].[Holiday]  WITH CHECK ADD  CONSTRAINT [CK_Holiday_Last] CHECK  ((isdate([Last])=(1) OR [Last]='Current'))
GO
ALTER TABLE [dbo].[Holiday] CHECK CONSTRAINT [CK_Holiday_Last]


INSERT INTO [Holiday]
           (
[NameID]
           ,
[Date]
           ,
[Days]
           ,
[IsPart]
           ,
[Offset]
           ,
[Last]
           ,
[First])
    
VALUES
           (
1
           ,
'0101'
           ,
1.0
           ,
0
           ,
0    
           ,
'20071231'
           ,
'20070101')

INSERT INTO [Holiday]
           (
[NameID]
           ,
[Date]
           ,
[Days]
           ,
[IsPart]
           ,
[Offset]
           ,
[Last]
           ,
[First])
    
VALUES
           (
2
           ,
'0218'
           ,
3.0
           ,
0
           ,
0    
           ,
'20071231'
           ,
'20070101')

INSERT INTO [Holiday]
           (
[NameID]
           ,
[Date]
           ,
[Days]
           ,
[IsPart]
           ,
[Offset]
           ,
[Last]
           ,
[First])
    
VALUES
           (
3
           ,
'0501'
           ,
3.0
           ,
0
           ,
0    
           ,
'20071231'
           ,
'20070101')

INSERT INTO [Holiday]
           (
[NameID]
           ,
[Date]
           ,
[Days]
           ,
[IsPart]
           ,
[Offset]
           ,
[Last]
           ,
[First])
    
VALUES
           (
4
           ,
'1001'
           ,
3.0
           ,
0
           ,
0    
           ,
'20071231'
           ,
'20070101')

INSERT INTO [Holiday]
           (
[NameID]
           ,
[Date]
           ,
[Days]
           ,
[IsPart]
           ,
[Offset]
           ,
[Last]
           ,
[First])
    
VALUES
           (
5
           ,
'0308'
           ,
0.5
           ,
1
           ,
0    
           ,
'Current'
           ,
'20070101')

INSERT INTO [Holiday]
           (
[NameID]
           ,
[Date]
           ,
[Days]
           ,
[IsPart]
           ,
[Offset]
           ,
[Last]
           ,
[First])
    
VALUES
           (
6
           ,
'0504'
           ,
0.5
           ,
1
           ,
0    
           ,
'Current'
           ,
'20070101')

INSERT INTO [Holiday]
           (
[NameID]
           ,
[Date]
           ,
[Days]
           ,
[IsPart]
           ,
[Offset]
           ,
[Last]
           ,
[First])
    
VALUES
           (
7
           ,
'0601'
           ,
1.0
           ,
1
           ,
0    
           ,
'Current'
           ,
'20070101')

INSERT INTO [Holiday]
           (
[NameID]
           ,
[Date]
           ,
[Days]
           ,
[IsPart]
           ,
[Offset]
           ,
[Last]
           ,
[First])
    
VALUES
           (
8
           ,
'0801'
           ,
0.5
           ,
1
           ,
0    
           ,
'Current'
           ,
'20070101')

INSERT INTO [Holiday]
           (
[NameID]
           ,
[Date]
           ,
[Days]
           ,
[IsPart]
           ,
[Offset]
           ,
[Last]
           ,
[First])
    
VALUES
           (
1
           ,
'0101'
           ,
1.0
           ,
0
           ,
-2   
           ,
'Current'
           ,
'20080101')

INSERT INTO [Holiday]
           (
[NameID]
           ,
[Date]
           ,
[Days]
           ,
[IsPart]
           ,
[Offset]
           ,
[Last]
           ,
[First])
    
VALUES
           (
2
           ,
'0207'
           ,
3.0
           ,
0
           ,
-1   
           ,
'Current'
           ,
'20080101')

INSERT INTO [Holiday]
           (
[NameID]
           ,
[Date]
           ,
[Days]
           ,
[IsPart]
           ,
[Offset]
           ,
[Last]
           ,
[First])
    
VALUES
           (
3
           ,
'0501'
           ,
1.0
           ,
0
           ,
0    
           ,
'Current'
           ,
'20080101')

INSERT INTO [Holiday]
           (
[NameID]
           ,
[Date]
           ,
[Days]
           ,
[IsPart]
           ,
[Offset]
           ,
[Last]
           ,
[First])
    
VALUES
           (
4
           ,
'1001'
           ,
3.0
           ,
0
           ,
-2   
           ,
'Current'
           ,
'20080101')

INSERT INTO [Holiday]
           (
[NameID]
           ,
[Date]
           ,
[Days]
           ,
[IsPart]
           ,
[Offset]
           ,
[Last]
           ,
[First])
    
VALUES
           (
9
           ,
'0404'
           ,
1.0
           ,
0
           ,
0    
           ,
'Current'
           ,
'20080101')

INSERT INTO [Holiday]
           (
[NameID]
           ,
[Date]
           ,
[Days]
           ,
[IsPart]
           ,
[Offset]
           ,
[Last]
           ,
[First])
    
VALUES
           (
10
           ,
'0608'
           ,
1.0
           ,
0
           ,
-1   
           ,
'Current'
           ,
'20080101')

INSERT INTO [Holiday]
           (
[NameID]
           ,
[Date]
           ,
[Days]
           ,
[IsPart]
           ,
[Offset]
           ,
[Last]
           ,
[First])
    
VALUES
           (
11
           ,
'0914'
           ,
1.0
           ,
0
           ,
-1   
           ,
'Current'
           ,
'20080101')




USE [Tmp]
GO
CREATE FUNCTION [dbo].[ChineseDatepart]
(
    
@Datepart varchar(5)
    ,
@Date datetime
)
RETURNS nvarchar(16)
AS
BEGIN
   
DECLARE @Result nvarchar(16)

   
IF @Datepart IN('weekday', 'dw')
       
SET @Result =
           
CASE DATEPART(dw, @Date)
               
WHEN 1 THEN '星期日'
               
WHEN 2 THEN '星期一'
               
WHEN 3 THEN '星期二'
               
WHEN 4 THEN '星期三'
               
WHEN 5 THEN '星期四'
               
WHEN 6 THEN '星期五'
               
WHEN 7 THEN '星期六'
           
END

   
IF @Datepart IN('ymd')
       
SET @Result = DATENAME(yyyy, @Date) + '' + DATENAME(m, @Date) + '' + RIGHT('0' + DATENAME(d, @Date), 2) + ''

   
IF @Datepart IN('md')
       
SET @Result = DATENAME(m, @Date) + '' + RIGHT('0' + DATENAME(d, @Date), 2) + ''

   
IF @Datepart IN('ymdw')
       
SET @Result = DATENAME(yyyy, @Date) + '' + DATENAME(m, @Date) + '' + RIGHT('0' + DATENAME(d, @Date), 2) + '日(' + [dbo].ChineseDatepart('dw', @Date) + ')'

   
IF @Datepart IN('mdw')
       
SET @Result = DATENAME(m, @Date) + '' + RIGHT('0' + DATENAME(d, @Date), 2) + '日(' + [dbo].ChineseDatepart('dw', @Date) + ')'

   
RETURN @Result
END



取一年的周六、日

SQL code
USE [Tmp] GO CREATE FUNCTION [dbo].[f_SunAndStas] ( @Year char(4) ) RETURNS @Table TABLE ( [ID] tinyint IDENTITY(1,1) ,[Date] smalldatetime ,[IsSunday] bit ) AS BEGIN DECLARE @Weeks tinyint ,@FirstDay smalldatetime ,@LastDay smalldatetime ,@FirstWeekday tinyint ,@FirstSunday smalldatetime ,@FirstSaturday smalldatetime SET @FirstDay = @Year + '0101' SET @FirstWeekday = DATEPART(weekday, @FirstDay) /*当年首日是星期几*/ SET @LastDay = @Year + '1231' SET @Weeks = DATEPART(week, @Year + '1231') /*当年的星期周数*/ --取当年第一个星期六、日相应的日期 IF @FirstWeekday = 1 BEGIN SET @FirstSunday = @FirstDay SET @FirstSaturday = DATEADD(d, 6, @FirstDay) END ELSE IF @FirstWeekday = 7 BEGIN SET @FirstSaturday = @FirstDay SET @FirstSunday = DATEADD(d, 1, @FirstSaturday) END ELSE BEGIN SET @FirstSaturday = DATEADD(d, 7 - @FirstWeekday, @FirstDay) SET @FirstSunday = DATEADD(d, 1, @FirstSaturday) END --取所有周六、日的相应日期 DECLARE @Index tinyint SET @Index =0 IF @FirstSaturday > @FirstSunday WHILE @Index < @Weeks - 1 BEGIN INSERT INTO @Table VALUES(DATEADD(week, @index, @FirstSunday), 1) INSERT INTO @Table VALUES(DATEADD(week, @index, @FirstSaturday), 0) SET @Index = @Index + 1 END ELSE WHILE @Index < @Weeks - 1 BEGIN INSERT INTO @Table VALUES(DATEADD(week, @index, @FirstSaturday), 0) INSERT INTO @Table VALUES(DATEADD(week, @index, @FirstSunday), 1) SET @Index = @Index + 1 END --处理最后一周是否有周六 DECLARE @LastSaturday smalldatetime SET @LastSaturday = DATEADD(week, @Index, @FirstSaturday) IF DATENAME(yyyy, @LastSaturday) = @Year INSERT INTO @Table VALUES(@LastSaturday, 0) RETURN END



USE [Tmp]
GO
CREATE FUNCTION [dbo].[f_ChineseHoliday]
(   
   
@Year char(4)
)
RETURNS TABLE
AS
RETURN
(
   
SELECT
        
[Name]
        ,
[Date] = @Year + [Date]
        ,
[From] = CONVERT(char(8), DATEADD(d, Offset, @Year + [Date]), 112)
        ,
[To] = CONVERT(char(8),
           
CASE
               
WHEN [Days] > 1 AND IsPart = 0 THEN DATEADD(d, [Days] + Offset + 4 -1, @Year + [Date])
               
WHEN [Days] = 1 AND IsPart = 0 THEN DATEADD(d, [Days] + Offset + 2 -1, @Year + [Date])
               
ELSE DATEADD(d, 0, @Year + [Date])
           
END , 112)
        ,
[Days]
        ,
[Total]=
           
CASE
               
WHEN [Days] > 1 AND IsPart = 0 THEN [Days] + 4
               
WHEN [Days] = 1 AND IsPart = 0 THEN [Days] + 2
               
ELSE [Days]
           
END
        ,
[IsPart]
        ,
[NameID] 
   
FROM [dbo].[f_Holiday](@Year + '0101')
)



USE [Tmp]
GO
CREATE FUNCTION [dbo].[ChineseDatepart]
(
    
@Datepart varchar(5) --5改成8或者7, len('weekday')=7
    ,@Date datetime
)
RETURNS nvarchar(16) AS
BEGIN
   
DECLARE @Result nvarchar(16)

   
IF @Datepart='weekday' OR @Datepart='dw'
       
RETURN CASE DATEPART(dw, @Date)
               
WHEN 1 THEN '星期日'
               
WHEN 2 THEN '星期一'
               
WHEN 3 THEN '星期二'
               
WHEN 4 THEN '星期三'
               
WHEN 5 THEN '星期四'
               
WHEN 6 THEN '星期五'
               
WHEN 7 THEN '星期六' END

   
IF @Datepart='ymd'
       
RETURN STUFF(STUFF(CONVERT(varchar(10), @Date, 120), 5, 1, ''), 8, 1, '')+''
       
--SET @Result = DATENAME(yyyy, @Date) + '年' + DATENAME(m, @Date) + '月' + RIGHT('0' + DATENAME(d, @Date), 2) + '日'

   
IF @Datepart IN('md')
       
SET @Result = DATENAME(m, @Date) + '' + RIGHT('0' + DATENAME(d, @Date), 2) + ''

   
IF @Datepart IN('ymdw')
       
SET @Result = DATENAME(yyyy, @Date) + '' + DATENAME(m, @Date) + '' + RIGHT('0' + DATENAME(d, @Date), 2) + '日(' + [dbo].ChineseDatepart('dw', @Date) + ')'

   
IF @Datepart IN('mdw')
       
SET @Result = DATENAME(m, @Date) + '' + RIGHT('0' + DATENAME(d, @Date), 2) + '日(' + [dbo].ChineseDatepart('dw', @Date) + ')'

   
RETURN @Result
END




posted @ 2009-07-24 13:40  yiyan21  阅读(359)  评论(0编辑  收藏  举报