Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search table by field that includes a quote

Status
Not open for further replies.

TalentedFool

Programmer
Jul 23, 2001
214
GB

Hi all

I'm trying to do a search on a table something like this

select * from table where field = 'this is the text's that I am searching for'


Obviously it throws it out due to single quote in the text. Noob question - how do I get around that?

Cheers

~ Remember - Nothing is Fool Proof to a Talented Fool ~
 

Sorry, I found the solution to the above .. what I now need it get around this using a sub query. ..

select * from table where field = select field2 from table2

Field2 might contain a single quote in it such as a '

Does that make sense?

It throws the quoted string not terminated properly at me

~ Remember - Nothing is Fool Proof to a Talented Fool ~
 
Put two single quotes together in the sting:

Code:
where field = 'this is the text''s that I am searching for'
 
For your second enquiry, you need a join or a sub-query

Code:
SELECT a.*
FROM   table  a,
       table2 b
WHERE a.field = b.field2;[code]

or

[code]SELECT *
FROM   table
WHERE  field = (SELECT field2
                FROM   table2);

Note the second solution above assumes you have only 1 row in table2. If not, change the = to IN.
 
Lewisp

I understand all that.

Sorry, this is not for me but a colleague and I've just had all the info. I might be in the wrong forum

what they are trying to do is on an ASP.net page is to build a string with a select command and run that against the database.

What he is trying to do is

Code:
string = 'select * from table1 where field = '" & searcher & "'"

Searcher might contain a single quote which is what is throwing him out

If you don't have a quick answer then I'll take this is a ASP.net forum

Thanks


~ Remember - Nothing is Fool Proof to a Talented Fool ~
 
I think you will simply need to find a way to pass two single quotes where there is one within a string.
 
Code:
string = "select * from table1 where field = '" & searcher & "'"

HTH

-Engi
 

Engineer. Thats - but that totally doesn't help :)

If searcher contains a single quote e.g "My searche's" it'll throw it out.

FYI - I found the solution. Since it was all done in VB.net I simply used the replace function so it now looks like

Code:
string = "select * from table1 where field1 = '" & replace(searcher,"'","''") & "'"

Works a treat! After that I scrapped it and started using parameters which, I'm not even going to get into.

Anyway - thanks for looking by ...

Subject Closed.

~ Remember - Nothing is Fool Proof to a Talented Fool ~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top