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
 Silly Question about %

Author  Topic 

DavidChel
Constraint Violating Yak Guru

474 Posts

Posted - 2008-07-23 : 16:21:27
I know the basics of using the Like operator and the use of the % sign. However, how does one search for a string that actually contains the literal % in it.

If I want to search for strings with actually start with "2%" and wanted to return records such as:

2% Fuel Charge
2% Off

But NOT
2 Dogs
2 Chickens

How would I do this?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-07-23 : 16:25:48
Use square brackets. Check out LIKE in BOL for details.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

DavidChel
Constraint Violating Yak Guru

474 Posts

Posted - 2008-07-23 : 16:31:02
All hail the Goddess.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-07-23 : 17:36:18
[code]
select
x
from
(
--Test Data
select x = '2%' union all
select x = '2% Fuel Charge' union all
select x = '2% Off' union all
select x = '2 Dogs' union all
select x = '2 Chickens'
) a
where
x like '2^%%' escape '^'


Results:
x
--------------
2%
2% Fuel Charge
2% Off

(3 row(s) affected)
[/code]

CODO ERGO SUM
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-07-24 : 02:35:38
or as Tara says

col like '2[%]%'

Madhivanan

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

DavidChel
Constraint Violating Yak Guru

474 Posts

Posted - 2008-07-24 : 09:17:33
Thanks for the input folks.
Go to Top of Page
   

- Advertisement -