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
 sql select query with 'like'

Author  Topic 

nirmala84
Starting Member

1 Post

Posted - 2010-01-27 : 05:05:29
hi all

I am new to this Sql Team Forum,I am Pleased to be here

I have an Issue in sql query,

Actually, the basic sql select query with 'like' we have as

Select * from tblname where description like '% n %'

But my issue is the description may be " It's Forum with the name as SQL Team" as like

With this description i cannot able to execute the query, because i am having single quotes within the description what i have to do for this to exec,

can anyone give me solution for this

If anyone knows please Help me..........

Thanks in advance
Regards
nirmla

EugeneLim11
Posting Yak Master

167 Posts

Posted - 2010-01-27 : 05:13:13
use parameters =)

Select * from tblname where description like @Parameters

This helps to prevent SQL injections attacks. :)
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-01-27 : 05:32:02
you are building this sql dynamically?

If so then certainly do as EugeneLim11 suggests.

Also -- check out the QUOTENAME function. it will escape all troublesome characters for you.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-27 : 05:37:16
"With this description i cannot able to execute the query, because i am having single quotes within the description what i have to do for this to exec"

Double up the single quotes:

Select * from tblname where description like '% It''s Forum with the name as SQL Team %'
Go to Top of Page
   

- Advertisement -