工算
由 日期 算 "前工"
工按月,不一月,超 15 天1 ...
DECLARE @D DateTime
SET @D = '2012-01-16'
SELECT Age = ????????????????????
感工算是容易有,容易生歧或件不明的地方。
[解决办法]
- SQL code
--写个最笨,但是最好理解的写法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+1endif(datediff(d,getdate(),@d)>15)set @i=@i-1select @i as age/*age-----------4*/
[解决办法]
- SQL code
DECLARE @D DateTimeSET @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 行受影响)*/
[解决办法]
- SQL code
--貌似这样就可以,楼主可以测试一下,有问题留言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*/
[解决办法]
[解决办法]
[解决办法]
- SQL code
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 试试这个