如何在嵌套的case语句中使用collat​​e进行区分大小写的搜索?

时间:2016-06-13 20:47:51

标签: sql-server

我目前正在运行一个查询,其中我正在寻找区分大小写的字母'%AR%'在一列内。还有其他单词在名称中包含字母,有时字母与另一个单词如#34; ARmart"配对,因此区分我所寻找的内容的唯一方法是逐个案例。这是我需要在 case when end_time is not null then 'completed' when status='S' or status='SR' then 'In Progress' else 'not completed' end as completed, case when status != 'D' then 'Error Occured' end as error,case when service like '%AR%' then 'Accounts Receivable' end as used_by中使用它的案例陈述。

1 个答案:

答案 0 :(得分:0)

默认情况下,like会返回AR的任何内容。这是一个例子:

declare @lower varchar (16), @mix varchar(16), @upper varchar(16)
set @lower = 'ddarcc'
set @mix = 'dDArcc'
set @upper = 'DDARCC'

select @lower where @lower like '%AR%'
select @mix where @mix like '%AR%'
select @upper where @upper like '%AR%'

要更改此设置,请将其设置为区分大小写:

declare @lower varchar (16), @mix varchar(16), @upper varchar(16)
set @lower = 'ddarcc'
set @mix = 'dDArcc'
set @upper = 'DDARCC'

select @lower where @lower  like '%AR%' COLLATE Latin1_General_CS_AS
select @mix Latin1_General_CS_AS where @mix like '%AR%' COLLATE Latin1_General_CS_AS
select @upper Latin1_General_CS_AS where @upper like '%AR%' COLLATE Latin1_General_CS_AS