由 日期 計算 "當前工齡" 工齡按月計,不滿一個月時,超過 15 天計1 ...DECLARE @D DateTime
SET @D = '2012-01-16'
SELECT Age = ???????????????????? 感覺工齡計算總是容易有問題,容易產生歧義或條件不夠明確的地方。

解决方案 »

  1.   

    DECLARE @D DateTime
    SET @D = '2012-01-16'
    SELECT Age = ???????????????????? 想問這個工齡計算的SQL語句如何寫。
      

  2.   

    --写个最笨,但是最好理解的写法
    declare @d datetime set @d = '2011-10-03'
    declare @i int set @i=0while(@d<getdate())
    begin
    set @d=dateadd(month,1,@d)
    set @i=@i+1
    endif(datediff(d,getdate(),@d)>15)
    set @i=@i-1select @i as age
    /*
    age
    -----------
    4
    */
      

  3.   

    DECLARE @D DateTime
    SET @D = '2011-01-16'
    select DATEDIFF(mm,@D,GETDATE())-1+  --月份掐头去尾
    (case when DATEDIFF(d,@d,convert(varchar(8),dateadd(mm,1,@d),120)+'01')+DATEDIFF(d,convert(varchar(8),getdate(),120)+'01',GETDATE())>15 then 1 else 0 end)
    /*
    -----------
    13(1 行受影响)*/
      

  4.   

    maco_wang(☆叶子☆) 的寫法雖對,但不敢這樣用啊。
    qianjin036a的好象有問題:
    SET @D = '2011-02-28' 時,返回11,而maco_wang(☆叶子☆) 的算法返回12。
    我是 2012-02-13晚測。我改了下,
    SELECT DATEDIFF(mm,@D,GETDATE()) + (case when (
    DATEDIFF(d,dateadd(mm,DATEDIFF(mm,@D,GETDATE())-1,@d) , GETDATE()) > 15 ) 
    then 0 else -1 end) 也不知道是否有問題(如閏年閏月等)。
      

  5.   

    第一个月剩余日期:
    DATEDIFF(d,@d,convert(varchar(8),dateadd(mm,1,@d),120)+'01')
    这里是拿第二个月的1号与参加工作的日期相差减,因此不管是否闰月,都是正确的.
      

  6.   


    --貌似这样就可以,楼主可以测试一下,有问题留言declare @d datetime set @d= '2011-10-03'
    declare @t datetime set @t= '2012-02-09'select datediff(month,@d,getdate())+ceiling((day(@t)-day(@d))/15) as age
    /*
    age
    -----------
    4
    */
      

  7.   


    --#9变量没放进来,修正一下
    declare @d datetime set @d= '2011-10-03'
    declare @t datetime set @t= '2012-02-09'select datediff(month,@d,@t)+ceiling((day(@t)-day(@d))/15) as age
    /*
    age
    -----------
    4
    */
      

  8.   

    另外,你的意思是 "超过15天",我理解为大于 15 天.如等于 15 天也算的话,将上面程序的 >15 改成 >=15.
      

  9.   

    select *,case when datediff(dd,begindate,getdate())%30<=15 
    then datediff(dd,begindate,getdate())/30 
    else datediff(dd,begindate,getdate())/30+1 end
    as [工龄(单位:月)] from emp
      

  10.   


    这种方法在这个案例中,不成立。
    declare @d datetime set @d= '2012-2-1'
    declare @t datetime set @t= '2012-03-31'可以改为:declare @d datetime set @d= '2012-2-1'
    declare @t datetime set @t= '2012-03-31'select  datediff(month,@d,@t)           +       
        CASE WHEN (DATEPART(d,@d) = 1 AND DATEPART(d,@t) = 31) THEN 1 ELSE  ceiling((day(@t)-day(@d))/15)    END      as age其实,应该还要校验@d<@t的。
      

  11.   


    --假设@t>@d,我把15改成16是不是就可以了?declare @d datetime set @d= '2011-03-01'
    declare @t datetime set @t= '2011-03-31'select datediff(month,@d,@t)+ceiling((day(@t)-day(@d))/16) as age
    /*
    age
    -----------
    1
    */
      

  12.   

    看了下貼,果然,這個工齡計算容易産生歧義。如何爲完整的一個月,有歧義:
    按qianjin036a的掐头去尾算法, 2011-2-28~2012-2-13,掐头1天,去尾13天,加起来14天,当然只有11个月了.
    可是按:2011-2-28--2012-1-28爲11個月整,2012-1-28--2012-2-13 就是16天了,也就算12個月了。
    如果按:2011-2-28--2012-2-28爲12個月整,2012-2-28--2012-2-13 就是-15天了,也就要減一個月。工資中工齡是很重要的一個參數,如何定義,確實傷腦筋。我自己都有些暈了。"工齡按月計,不滿一個月時,超過 15 天計1 否則計0" 看起來明確的語義,原來卻是可以坑人的玩意。 
      

  13.   


    declare @d datetime set @d= '2011-02-28'
    declare @t datetime set @t= '2011-03-30'select case when day(@d)< 15 and day(@t)>=15   then datediff(mm,@d,@t)+ (case when abs(day(@d)-day(@t))>=15 then 1 else 0 end)
    when day(@d)< 15 and day(@t)< 15   then datediff(mm,@d,@t)
    when day(@d)>= 15 and day(@t)< 15  then datediff(mm,@d,@t)
    when day(@d)>= 15 and day(@t)>= 15 then datediff(mm,@d,@t)+ (case when abs(day(@d)-day(@t))>=15 then 1 else 0 end)
    end as age 试试这个