x

Understanding PATINDEX query

I am new to SQL server 2000 and 2005. Can anyone tell me why I am getting wrong results while executing the following simple queries

Case 1

SELECT PATINDEX('%[áčďéěíňóřšťúůýžÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]%', 'This is a test')

Ans: 1

Case 2

SELECT PATINDEX('%[áčďéěíňóřšťúůýžÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]%', 'This is a teŠt')

Ans: 1

Case 3

SELECT PATINDEX('%[áčďéěíňóřšťúůýžÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]%', 'his is a teŠt')

Ans: 10 (Deleted "T" from the word "This" and got the Correct answer)

more ▼

asked Nov 09, 2009 at 09:20 AM in Default

avatar image

SQLserver_novice
113 6 6 9

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

You need to specify the strings as unicode strings, and use a collation that is at least case and accent sensitive. I have chosen the most sensitive Latin1 code page available for this query:

SELECT PATINDEX(N'%[áčďéěíňóřšťúůýžÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]%', N'This is a test' COLLATE Latin1_General_100_CS_AS_KS_WS)
UNION ALL
SELECT PATINDEX(N'%[áčďéěíňóřšťúůýžÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]%', N'This is a teŠt' COLLATE Latin1_General_100_CS_AS_KS_WS)
UNION ALL
SELECT PATINDEX(N'%[áčďéěíňóřšťúůýžÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]%', N'his is a teŠt' COLLATE Latin1_General_100_CS_AS_KS_WS)

The results from this query are:

  • 0
  • 13
  • 12

Also, note that your case 3 answer was not correct - it was returning the index of the first 't'.

more ▼

answered Nov 09, 2009 at 09:45 AM

avatar image

Matt Whitfield ♦♦
29.5k 62 66 88

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1069
x24

asked: Nov 09, 2009 at 09:20 AM

Seen: 3161 times

Last Updated: Nov 09, 2009 at 09:50 AM

Copyright 2016 Redgate Software. Privacy Policy