日期:2014-05-18 浏览次数:20544 次
select *,
a1=case when date1<dateadd(day,180,getdate()) then 'Y' else 'N' end,
a2=case when date2<dateadd(day,180,getdate()) then 'Y' else 'N' end
from tb
------解决方案--------------------
create table tb(id int,date1 datetime,date2 datetime)
insert into tb
select 1,'2011-11-08',null union
select 2,null,'2011-10-03'
select *,
a1=case when date1<dateadd(day,180,getdate()) then 'Y' else 'N' end,
a2=case when date2<dateadd(day,30,getdate()) then 'Y' else 'N' end
from tb
/*
id date1 date2 a1 a2
----------- ----------------------- ----------------------- ---- ----
1 2011-11-08 00:00:00.000 NULL Y N
2 NULL 2011-10-03 00:00:00.000 N Y
(2 行受影响)
------解决方案--------------------
select *,
a1=case when datediff(day,date1,getdate())>180 then 'Y' else 'N' end,
a2=case when datediff(day,date1,getdate())>180 then 'Y' else 'N' end
from tb
------解决方案--------------------
楼主的结果不对吧
if object_id('test') is not null
drop table test
go
create table test
(
id int identity(1,1),
date1 datetime,
date2 datetime
)
go
insert into test(date1,date2)
select '2011-11-08',null union all
select null,'2011-10-03' union all
select null,null union all
select '2011-10-09','2011-11-08' union all
select '2012-01-09','2017-11-07'
go
select * ,
a1=case when date1<dateadd(dd,180,getdate()) then 'Y' else 'N' end,
a2=case when date2<dateadd(dd,30,getdate()) then 'Y' else 'N' end
from test
go
/*
id date1 date2 a1 a2
----------- ----------------------- ----------------------- ---- ----
1 2011-11-08 00:00:00.000 NULL Y N
2 NULL 2011-10-03 00:00:00.000 N Y
3 NULL NULL N N
4 2011-10-09 00:00:00.000 2011-11-08 00:00:00.000 Y Y
5 2012-01-09 00:00:00.000 2017-11-07 00:00:00.000 Y N
(5 行受影响)
*/