现成的函数:select DATEDIFF (d, '2004-05-06 ', '2004-07-12')--结果:
----------- 
67(所影响的行数为 1 行)

解决方案 »

  1.   

    如果在表里,这样select DATEDIFF (d, cast(开始时间 as datetime), cast(结束时间 as datetiem) as 请假天数 FROM 表 where 员工工号=1
      

  2.   

    --直接用datediff就可以了:select datediff(day,开始时间,结束时间)+1  --应该要+1才对
    from 表
      

  3.   

    --如果是要每个月一条记录独立,则可以用:select a.员工工号
    ,开始时间=convert(char(10)
    ,case b.id when 0 then 开始时间 
    else dateadd(month,b.id,dateadd(day,1-day(开始时间),开始时间)) end
    ,120)
    ,结束时间=convert(char(10)
    ,case b.id when datediff(month,a.开始时间,a.结束时间) then 结束时间 
    else dateadd(month,b.id+1,dateadd(day,1-day(开始时间),开始时间))-1 end
    ,120)
    ,请假天数=datediff(day,
    case b.id when 0 then 开始时间 
    else dateadd(month,b.id,dateadd(day,1-day(开始时间),开始时间)) end
    ,case b.id when datediff(month,a.开始时间,a.结束时间) then 结束时间 
    else dateadd(month,b.id+1,dateadd(day,1-day(开始时间),开始时间)) end
    )
    from 表 a join (
    select id=a.id+b.id
    from(
    select id=0 union all select 1
    union all select id=2 union all select 3
    union all select id=4 union all select 5
    union all select id=6 union all select 7
    union all select id=8 union all select 9
    ) a,(
    select id=0 union all select 10
    union all select id=20 union all select 30
    union all select id=40 union all select 50
    union all select id=60 union all select 70
    union all select id=80 union all select 90
    ) b
    )b on b.id<=datediff(month,a.开始时间,a.结束时间)
      

  4.   

    --测试--测试数据
    create table 表(员工工号 int,开始时间 varchar(10),结束时间 varchar(10))
    insert 表 select 1,'2004-05-06','2004-07-12'
    union all select 2,'2004-05-11','2004-06-02'
    union all select 3,'2004-05-11','2004-05-22'
    go--如果是要每个月一条记录独立,则可以用:select a.员工工号
    ,开始时间=convert(char(10)
    ,case b.id when 0 then 开始时间 
    else dateadd(month,b.id,dateadd(day,1-day(开始时间),开始时间)) end
    ,120)
    ,结束时间=convert(char(10)
    ,case b.id when datediff(month,a.开始时间,a.结束时间) then 结束时间 
    else dateadd(month,b.id+1,dateadd(day,1-day(开始时间),开始时间))-1 end
    ,120)
    ,请假天数=datediff(day,
    case b.id when 0 then 开始时间 
    else dateadd(month,b.id,dateadd(day,1-day(开始时间),开始时间)) end
    ,case b.id when datediff(month,a.开始时间,a.结束时间) then 结束时间 
    else dateadd(month,b.id+1,dateadd(day,1-day(开始时间),开始时间)) end
    )
    from 表 a join (
    select id=a.id+b.id
    from(
    select id=0 union all select 1
    union all select id=2 union all select 3
    union all select id=4 union all select 5
    union all select id=6 union all select 7
    union all select id=8 union all select 9
    ) a,(
    select id=0 union all select 10
    union all select id=20 union all select 30
    union all select id=40 union all select 50
    union all select id=60 union all select 70
    union all select id=80 union all select 90
    ) b
    )b on b.id<=datediff(month,a.开始时间,a.结束时间)
    go--删除测试
    drop table 表/*--测试结果员工工号     开始时间    结束时间     请假天数        
    ----------- ------------ ------------ ----------- 
    1           2004-05-06   2004-05-31   26
    1           2004-06-01   2004-06-30   30
    1           2004-07-01   2004-07-12   11
    2           2004-05-11   2004-05-31   21
    2           2004-06-01   2004-06-02   1
    3           2004-05-11   2004-05-22   11(所影响的行数为 6 行)
    --*/
      

  5.   

    不对
    需要排班数据,来确定那些天应该上班
    应该上班的才算做请假至于按月统计,用convert就可以
      

  6.   

    T-SQL 生成 两个新的真正的公历年历
    http://www.csdn.net/Develop/read_article.asp?id=26447T-SQL 生成一个简易的 公历年历 T-SQL 含日期所在月及年的周次
    http://www.csdn.net/Develop/read_article.asp?id=26083