摘要:
下文讲述sqlserver检索数据时,使用case when then对查询条件进行判断的方法,如下所示:
实验环境:sql server 2008 R2
在平常我们使用case when then 对数据进行展示区别,如下所示:
---方法1: case when 条件1 then 结果1 when 条件2 then 结果2 else 其它 end ---方法2: case 字段 when '值1' then '结果1' when '值2' then '结果2' else 其它 end
例:
select id, case when info ='a' then 'b' else '其它' end as info from tableName
当我们需要使用case when end 作为where检索的条件值时,我们可以通过将条件放入when中,case的返回值和where条件中的做对比,如下所示:
select id, case when info ='a' then 'b' else '其它' end as info from tableName where ( case when info like '%test%' then 1 else 0 end )=1