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 2008 Forums
 Transact-SQL (2008)
 Similar search

Author  Topic 

tech1
Starting Member

49 Posts

Posted - 2011-07-18 : 18:08:22
Just a quick one.

lets say I have a table with records. We are searching on a field which is of nvarchar.

we are doing a LIKE search.

SELECT * FROM MyTable WHERE Field LIKE '%Shes the one%'

Problem: What if the "Field" contains "She's the one" - notice the apostrophe.
is there a way to query it and say to ignore the punctuation, rather than modifying the underlaying data?

Thanks!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-18 : 18:12:41
You should escape it:
SELECT * FROM MyTable WHERE Field LIKE '%She''s the one%'

Performance is going to suffer for a query like that. Better to only allow a "starts with" search. Or look into full-text search.

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

Subscribe to my blog
Go to Top of Page

tech1
Starting Member

49 Posts

Posted - 2011-07-18 : 18:58:57
thanks. well its ok as the table is very very small. we are talking <1000 records.

The problem with escaping is that this will be coming from a web application and using SPROCS to pass in params from which the query will be using to query against in the WHERE Clause.

so really I want to "escape" the keyword AND the field without modifying the underlaying data.

UDF?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-07-19 : 04:08:31
You can escape the character in the web application and send the data as parameter

Madhivanan

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

- Advertisement -