I have a single table for which I wish to find all the instances where 0 is the last character in a string field. I tried this by using two methods and they each returned different results.
This first query seemed to actually return the correct answer
select * from icd
where CHARINDEX('0',icd,LEN(icd)) =LEN(icd)
this one does not catch all of the answers
select * from icd
where PATINDEX('%0%',icd) = LEN(icd)
using
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
I found the set of data that CHARINDEX picked up that PATINDEX did not. I even did
update icd
set ICD=RTRIM(icd)
Why would PATINDEX indiscriminately leave out some rows?
Because you have a 0 somewhere before the end.
Change
PATINDEX('%0%',icd)toPATINDEX('%0',icd)