六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 48|回复: 0

SQLServer2008 在where条件中使用CASE WHEN

[复制链接]

升级  49.33%

34

主题

34

主题

34

主题

秀才

Rank: 2

积分
124
 楼主| 发表于 2013-1-26 12:41:40 | 显示全部楼层 |阅读模式
create table #temp
(
    id int identity(1,1),
    name varchar(20),
    startYear int,
    startMonth int
)
insert into #temp
select 'z','2010','12' union all
select 'e','2011','11' union all
select 'a','2011','2' union all
select 'b','2011','5' union all
select 'c','2011','10' union all
select 'd','2011','7'
--查询早于等于指定日期的数据
declare @condition varchar(6)
set @condition='201110'
select * from #temp A
where
 (case when LEN(A.startMonth)=1 and A.startMonth!=0 then cast((CAST(startYear as varchar(4))+'0'+cast(A.startMonth as varchar(1))) as date)
       when LEN(A.startMonth)=2 then cast(CAST(startYear as varchar(4))+cast(A.startMonth as varchar(2)) as date) end) <=cast(@condition as date)

 
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表