Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Searching for "_" in a string

Author  Topic 

rmg1
Constraint Violating Yak Guru

256 Posts

Posted - 2011-04-08 : 07:45:20
Hi all

I've got some character strings which contain a "_" (which is normally a wildcard).

I want to be able to search for things like "_OH_" as a string and treat the "_" as part of the string not as a wildcard.

Anyone any ideas?

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-04-08 : 07:48:18
put square brackets around it:
where col1 like '%[_]OH[_]%'


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2011-04-08 : 07:51:48
Or
WHERE PATINDEX('_OH_', col) > 0;


______________________
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-04-08 : 07:56:15
quote:
Originally posted by ms65g

Or
WHERE PATINDEX('%_OH_%', col) > 0;


______________________





No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

rmg1
Constraint Violating Yak Guru

256 Posts

Posted - 2011-04-08 : 07:56:23
Thanks folks, works perfectly.
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2011-04-08 : 08:02:08
Replace patindex with charindex

______________________
Go to Top of Page
   

- Advertisement -