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)
 Searching data which has underscore letter

Author  Topic 

venkath
Posting Yak Master

202 Posts

Posted - 2008-09-26 : 07:03:15
Hi all

The data in the column is as follows

COL
----
pr_view1
prview2
pr_view3

I want to search the data that starts with 'pr_%'

For the query
select col from tablw where col like 'pr_%'
I am getting all the 3 records.

Please help me how to search data with underscore letter

Thanks


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-26 : 07:09:57
select col from tablw where col like 'pr%' and charindex('_',col)>0
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-09-26 : 07:18:18
You need to escape it

where col like 'pr[_]%'

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-09-26 : 07:20:15
quote:
Originally posted by visakh16

select col from tablw where col like 'pr%' and charindex('_',col)>0


That wont do the job exactly and would fail for the data like prvi_ew2, prview_2,etc

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-26 : 07:23:21
quote:
Originally posted by madhivanan

quote:
Originally posted by visakh16

select col from tablw where col like 'pr%' and charindex('_',col)>0


That wont do the job exactly and would fail for the data like prvi_ew2, prview_2,etc

Madhivanan

Failing to plan is Planning to fail


yup...it wont work in all cases
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-09-26 : 07:30:32
Other method is to use escape keyword

where col like 'pr__%' escape '_'


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

venkath
Posting Yak Master

202 Posts

Posted - 2008-09-26 : 08:22:50
Thank you all for your response
Go to Top of Page
   

- Advertisement -