【发布时间】:2012-10-23 15:58:34
【问题描述】:
我有一个表,我希望找到所有实例,其中0 是字符串字段中的 last 字符。我通过使用两种方法进行了尝试,它们都返回了不同的结果。
第一个查询似乎实际上返回了正确答案
select * from icd
where CHARINDEX('0',icd,LEN(icd)) =LEN(icd)
这一个没有抓住所有的答案
select * from icd
where PATINDEX('%0%',icd) = LEN(icd)
使用
select t.ICD as theCharIndex,x.ICD as thePatIndex from
(
select * from icd
where CHARINDEX('0',icd,LEN(icd)) =LEN(icd)
) t
left join
(
select * from icd
where PATINDEX('%0%',icd) = LEN(icd)
) x on x.ICD=t.ICD
where x.ICD is null
我找到了CHARINDEX 收集到的一组数据,而PATINDEX 没有。我什至做到了
update icd
set ICD=RTRIM(icd)
为什么PATINDEX 会不加选择地漏掉一些行?
【问题讨论】:
-
使用
Right( icd, 1 ) = '0'可能更清晰。 -
它并不性感,但它肯定更简洁:)
标签: sql-server-2008 tsql