SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to find PATINDEX for [


How to find PATINDEX for [

Author
Message
Ulysses_Infinity
Ulysses_Infinity
SSC Eights!
SSC Eights! (873 reputation)SSC Eights! (873 reputation)SSC Eights! (873 reputation)SSC Eights! (873 reputation)SSC Eights! (873 reputation)SSC Eights! (873 reputation)SSC Eights! (873 reputation)SSC Eights! (873 reputation)

Group: General Forum Members
Points: 873 Visits: 664
Hi all,

How can I find the character position of the first instance of the character [ in a text string. The following doesn't work:

PATINDEX('%[%',mytextfield)

although it does work for the ] character.

Any help greatly appreciated. (I appreciated this is probably to do with the [] wildcard functionality)

rgds - Jason
LutzM
LutzM
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10301 Visits: 13559
Since [ is a meta-character in wildcard expressions, you'll need to wrap it with another set of brackets:
SELECT PATINDEX('%[[]%',mytextfield)





Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Ulysses_Infinity
Ulysses_Infinity
SSC Eights!
SSC Eights! (873 reputation)SSC Eights! (873 reputation)SSC Eights! (873 reputation)SSC Eights! (873 reputation)SSC Eights! (873 reputation)SSC Eights! (873 reputation)SSC Eights! (873 reputation)SSC Eights! (873 reputation)

Group: General Forum Members
Points: 873 Visits: 664
Thanks Lutz. Appreciate the help. - you helped where google and BOL etc. failed!

Jason
----
LutzM
LutzM
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10301 Visits: 13559
As usual, google will return some helpful info as long as you know the "magic spell".
in this case, "patindex escape" would have provided a number of helpful links. ;-)

I agree regarding BOL though. The description provided for PATINDEX is kinda weak regarding the handling of meta-characters.



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
davoscollective
davoscollective
SSC Eights!
SSC Eights! (927 reputation)SSC Eights! (927 reputation)SSC Eights! (927 reputation)SSC Eights! (927 reputation)SSC Eights! (927 reputation)SSC Eights! (927 reputation)SSC Eights! (927 reputation)SSC Eights! (927 reputation)

Group: General Forum Members
Points: 927 Visits: 1004
LutzM (10/4/2011)
Since [ is a meta-character in wildcard expressions, you'll need to wrap it with another set of brackets:
SELECT PATINDEX('%[[]%',mytextfield)



Interestingly, that same pattern doesn't work for the closing square bracket (I'm on 2012).

Works:
DECLARE @strPattern nvarchar(10)
SELECT @strPattern = '%[[]%'
SELECT '123]456', PATINDEX(@strPattern, '123[456')



Doesn't work:
DECLARE @strPattern nvarchar(10)
SELECT @strPattern = '%[]]%'
SELECT '123]456', PATINDEX(@strPattern, '123]456')


Eirikur Eiriksson
Eirikur Eiriksson
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15092 Visits: 18597
You are better off using the CHARINDEX function, and CHAR or NCHAR functions when dealing with special characters.
Cool

DECLARE @TEST_STR NVARCHAR(128) = N'ASDFGHJ[QWER';
SELECT CHARINDEX(NCHAR(91),@TEST_STR) AS POS



Result
POS
----
8

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search