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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 like %_%

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2010-03-24 : 09:39:30
Hello,
tblMain has a field called Code which is varchar(20)
I am running the following query to retrieve the records which have a code with an underscore in it i.e. 3232_5454_54545

select * from tblMain where Code like '%_%'
But this query returns everything including the codes which do not have underscores.
Do you see what I am doing wrong please?
Thanks

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-03-24 : 09:43:59
Hi,

You need to use :
like '%[_]%'

Example:


Select * from(
select '3232_5454_54545' as RES
union
select '3232545454545' as RES
) as subTAb where Res like '%[_]%'


Regards,
Bohra
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-03-24 : 09:44:03
The underscore is a wildcard for one character.
Try '%[_]%'


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

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-03-24 : 09:44:31



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

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2010-03-24 : 09:51:07
Solved.
Thank you all.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-03-24 : 12:59:19
"You need to use :
like '%[_]%'
"

or

like '%\_%' ESCAPE '\'
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-03-24 : 15:44:38
For better understanding:
like '%\_%' ESCAPE '\'

that ESCAPE '\' isn't a doku - it is part of the statement!


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

- Advertisement -