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)
 How to search for a string that has a ' character

Author  Topic 

zeeshan13
Constraint Violating Yak Guru

347 Posts

Posted - 2009-08-26 : 09:54:54
In T-SQL some characters like ' are reserve, for example if I want to search a name of a student which contian the keyword John I would write the following in my SELECT query.

Select * from Student where name like 'John'


Now what if I want to search a name that has the ' character, for example the name M'Cafe.

Please suggest a select query for it.

Thanks,

Zee















zeeshan13
Constraint Violating Yak Guru

347 Posts

Posted - 2009-08-26 : 09:59:30
Any quick help would be highly appreciated.
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-08-26 : 10:02:59
Use double '

select * from Student where [name] like '%''%'
Go to Top of Page

cat_jesus
Aged Yak Warrior

547 Posts

Posted - 2009-08-26 : 10:03:41
CREATE TABLE #temp
(
Userid VARCHAR(15)
,[Type] VARCHAR(5)
)

INSERT INTO #temp
SELECT 'A','x'''

SELECT * FROM #temp WHERE [TYPE] LIKE '%''%'



An infinite universe is the ultimate cartesian product.
Go to Top of Page

maxbet
Starting Member

5 Posts

Posted - 2009-08-26 : 10:04:17
quote:
Originally posted by zeeshan13

In T-SQL some characters like ' are reserve, for example if I want to search a name of a student which contian the keyword John I would write the following in my SELECT query.

Select * from Student where name like 'John'


Now what if I want to search a name that has the ' character, for example the name M'Cafe.

Please suggest a select query for it.



Thanks,

Zee




















Select * from Student where name like 'M''Cafe'
Go to Top of Page

zeeshan13
Constraint Violating Yak Guru

347 Posts

Posted - 2009-08-26 : 18:45:41
Thanks everyone for the quick help. It worked :)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-08-27 : 02:03:49
Also read http://sqlblogcasts.com/blogs/madhivanan/archive/2008/02/19/understanding-single-quotes.aspx

Madhivanan

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

- Advertisement -