日期:2014-05-17 浏览次数:20569 次
select Cus_Mobile,Date_Daoqi,
case when datediff(month,getdate(),f_date) < 0 then '过期' else '未到期'+cast(datediff(month,getdate(),f_date)+1 as varchar)+'个月' end A from T
if object_id('[TB]') is not null drop table [TB]
create table [TB]([Cus_Mobile] bigint,[Date_Daoqi] datetime)
insert[TB]
select 13800123545,'2012-01-01' union all
select 13101055858,'2014-01-15' union all
select 18912345678,null union all
select 18102012345,'2015-01-01'
select * from [TB]
SELECT * ,
[AA] = CASE WHEN date_daoqi < DATEADD(mm,-1,GETDATE()) THEN '已到期'
WHEN date_daoqi IS NULL THEN '未知'
ELSE '未到期' + CONVERT(VARCHAR, DATEDIFF(mm, DATEADD(mm,-1,GETDATE()),
date_daoqi))
+ '个月'
END
FROM dbo.TB
/*
Cus_Mobile Date_Daoqi AA
13800123545 2012-01-01 00:00:00.000 已到期
13101055858 2014-01-15 00:00:00.000 未到期1个月
18912345678 NULL 未知
18102012345 2015-01-01 00:00:00.000 未到期13个月*/