22,209
社区成员
发帖
与我相关
我的任务
分享
select id, ETA, LastUpdate
,case when Month(LastUpdate)=12
and Month(LastUpdate) >= cast(substring(ETA, 1, charindex('-',ETA) - 1) as int)
and Day(LastUpdate) >= cast(substring(ETA, charindex('-',ETA) + 1, charindex(' ',ETA)-charindex('-',ETA)) as int)
then convert(datetime, cast(Year(LastUpdate)+1 as varchar)+'-'+ETA, 121)
else convert(datetime, cast(Year(LastUpdate) as varchar)+'-'+ETA, 121) end ETADate
from @t
declare @t table (ID int, ETA varchar(20), LastUpdate datetime)
insert into @t
select 1, '5-27 9:0', '2010-05-25 18:49:09' union all
select 2, '12-30 23:0', '2010-12-28 18:19:09' union all
select 3, '1-2 13:0', '2010-12-24 0:5:27' union all
select 4, '1-5 4:30', '2010-12-27 0:23:27' union all
select 5, '5-27 9:0', '2009-05-25 18:49:09' union all
select 6, '12-30 23:0', '2011-12-28 18:19:09' union all
select 7, '1-2 13:0', '2012-12-24 0:5:27'
select id, ETA, LastUpdate
,case when Month(LastUpdate)=12 then convert(datetime, cast(Year(LastUpdate)+1 as varchar)+'-'+ETA, 121)
else convert(datetime, cast(Year(LastUpdate) as varchar)+'-'+ETA, 121) end ETADate
from @t
/*
--效果
id ETA LastUpdate ETADate
1 5-27 9:0 2010-05-25 18:49:09.000 2010-05-27 09:00:00.000
2 12-30 23:0 2010-12-28 18:19:09.000 2011-12-30 23:00:00.000
3 1-2 13:0 2010-12-24 00:05:27.000 2011-01-02 13:00:00.000
4 1-5 4:30 2010-12-27 00:23:27.000 2011-01-05 04:30:00.000
5 5-27 9:0 2009-05-25 18:49:09.000 2009-05-27 09:00:00.000
6 12-30 23:0 2011-12-28 18:19:09.000 2012-12-30 23:00:00.000
7 1-2 13:0 2012-12-24 00:05:27.000 2013-01-02 13:00:00.000
*/
declare @t table (ID int, ETA varchar(20), LastUpdate datetime)
insert into @t
select 1, '5-27 9:0', '2010-05-25 18:49:09' union all
select 2, '12-30 23:0', '2010-12-28 18:19:09' union all
select 3, '1-2 13:0', '2010-12-24 0:5:27' union all
select 4, '1-5 4:30', '2010-12-27 0:23:27' union all
select 5, '5-27 9:0', '2009-05-25 18:49:09' union all
select 6, '12-30 23:0', '2011-12-28 18:19:09' union all
select 7, '1-2 13:0', '2012-12-24 0:5:27'
select id, ETA, convert(datetime, cast(Year(LastUpdate)+1 as varchar)+'-'+ETA, 121) ETADate, LastUpdate from @t