Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to use PATINDEX with a variable containing '[' and ']'


How to use PATINDEX with a variable containing '[' and ']'

Author
Message
chris.asaipillai-624309
chris.asaipillai-624309
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 155
Ok my questions is how do i search for a variable which contains '[' and ']' in another string using PATINDEX

I have the following example:

DECLARE @filename CHAR(64)

DECLARE @FILEPATH VARCHAR(MAX)

SET @filename = 'ASTERIX [Converted].eps.ai'

SET @FILEPATH = ':Volumes:Art WIP: CHILDRENS:ASTERIX: ALBUMS USING NEW FRENCH ARTWORK:20 - Folder:Links:ASTERIX [Converted].eps.ai'

When i try : SELECT PATINDEX('%[[]' + @filename + '][]%',@FILEPATH)

I get 0 returned when you see clearly that @filename exists in @FILEPATH.
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8564 Visits: 18143
Is the bold part an error on your post or on your query?
SELECT PATINDEX('%[[]' + @filename + '][]%',@FILEPATH)



Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
chris.asaipillai-624309
chris.asaipillai-624309
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 155
the whole expression returns 0....try it as follows:

DECLARE @filename CHAR(64)

DECLARE @FILEPATH VARCHAR(MAX)

SET @filename = 'ASTERIX [Converted].eps.ai'
SET @FILEPATH = ':Volumes:Art WIP: CHILDRENS:ASTERIX: ALBUMS USING NEW FRENCH ARTWORK:20 - Asterix in Corsica:9780752866444_Asterix Corsica PB Folder:Links:ASTERIX [Converted].eps.ai'

SELECT PATINDEX('%[[]' + @filename + '][]%',@FILEPATH)
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8564 Visits: 18143
This will return the correct results but only if you change your filename data type to varchar:


SELECT CHARINDEX(@filename,@FILEPATH), PATINDEX('%' + REPLACE( @filename, '[', '[[]') + '%',@FILEPATH)



This should show you the difference

DECLARE @filename varCHAR(64)
SET @filename = 'ASTERIX [Converted].eps.ai'
SELECT '%' + @filename + '%'
GO
DECLARE @filename CHAR(64)
SET @filename = 'ASTERIX [Converted].eps.ai'
SELECT '%' + @filename + '%'
GO




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
chris.asaipillai-624309
chris.asaipillai-624309
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 155
Brilliant that works for me.

thank you so much Luis!:-)
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