我有一个日期表和一个 ID、FromDate 和 ToDate 列,如下所示
ID FromDate ToDate
-- -------------- --------------
1 2013-06-10 00:00:00 2013-06-30 00:00:00
2 2013-05-10 00:00:00 2013-06-10 00:00:00
3 2012-08-01 00:00:00 2012-12-31 00:00:00
4 2013-07-10 00:00:00 2013-07-30 00:00:00
我进行检查查询并使用如下所示的 [getdate] 将列作为 Result 是否处于活动、非活动或过期
Select ID, FromDate, ToDate,
(case when convert(varchar(8),FromDate,112) <= convert(varchar(8),getdate(),112)
and convert(varchar(8),ToDate,112) <= convert(varchar(8),getdate(),112) then 'Expired'
when convert(varchar(8),FromDate,112) <= convert(varchar(8),getdate(),112)
and convert(varchar(8),ToDate,112) >= convert(varchar(8),getdate(),112) then 'Active'
when convert(varchar(8),FromDate,112) >= convert(varchar(8),getdate(),112)
and convert(varchar(8),ToDate,112) >= convert(varchar(8),getdate(),112) then 'Inactive' end )'Result'
from Date
它将显示正确的结果
ID FromDate ToDate Result
-- -------------- -------------- --------
1 2013-06-10 00:00:00 2013-06-30 00:00:00 Active
2 2013-05-10 00:00:00 2013-06-10 00:00:00 Expired
3 2012-08-01 00:00:00 2012-12-31 00:00:00 Expired
4 2013-07-10 00:00:00 2013-07-30 00:00:00 Inactive
但是我的Case查询太长了,有没有办法缩短代码或者提高性能呢?