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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL query with apostrophe 4

Status
Not open for further replies.

rgordley

Programmer
Apr 30, 1999
63
US
A SQL query of the type:<br>
&quot;select * from sometable where name = 'O'conner' &quot; <br>
generates an error because of the nested apostrophes<br>
does anyone know the syntax to correct this?
 
&quot;select * from sometable where name = 'O''conner' &quot; <br>
<br>
or<br>
<br>
&quot;select * from sometable where name = &quot; & chr(39) & &quot;O'conner&quot; & chr(39) <br>
<p>nick bulka<br><a href=mailto:nick@bulka.com>nick@bulka.com</a><br><a href= > </a><br>
 
<br>
rgordley,<br>
<br>
I tried Nick's code and I can't get either method to work -- Did you try it?
 
The function SqS returns a proper SQL expression for any string - e.g. SqS(&quot;O'Connor&quot;) -&gt; 'O''Connor'<br>
<br>
SQL= &quot;Select * from Tbl where Name = &quot; & SqS(&quot;O'Connor&quot;)<br>
<br>
Cut and paste this code into VB IDE to read it - &quot; and '' look the same in this font.<br>
<br>
Public Function SqS(Element) As String<br>
SqS = &quot;'&quot; & FindAndReplace(CStr(Element), &quot;'&quot;, &quot;''&quot;) & &quot;'&quot;<br>
End Function<br>
<br>
Public Function FindAndReplace(StringToExamine As String, Replace As String, WithThis As String) As _<br>
String<br>
Dim Work As String<br>
Dim Found As String<br>
Dim ix As Integer<br>
Work = StringToExamine<br>
Do While Len(Work) &gt; 0<br>
ix = ThisInThatText(Replace, Work)<br>
If ix = 0 Then<br>
Found = Found & Work<br>
Work = &quot;&quot;<br>
Else<br>
Found = Found & Left$(Work, ix - 1) & WithThis<br>
Work = Mid$(Work, ix + Len(Replace))<br>
End If<br>
Loop<br>
FindAndReplace = Found<br>
End Function<br>
<br>
Public Function ThisInThatText(ThisString As String, InThatString As String, Optional _<br>
StartPositionInThatString = 1) As Long<br>
ThisInThatText = InStr(StartPositionInThatString, InThatString, ThisString, vbTextCompare)<br>
End Function<br>
<br>
<p>Roy Lofquist<br><a href=mailto:roylofquist@msn.com>roylofquist@msn.com</a><br><a href= > </a><br>
 
VB400, <br>
I couldn't either, but using that idea I came up with this<br>
<br>
customername=&quot;O&quot; & chr(39) & chr(39) & &quot;Connor&quot;<br>
<br>
strSQL=&quot;select * from sometable where name = '&quot; & customername & &quot;' &quot;<br>
<br>
This works....so thanks nick for getting me on the right track.<br>

 
<br>
rgordley,<br>
<br>
You are on the same track as Roy. However, Roy's solution works for any name with an apostrophe (not just O'Conner). Additionally, if the name has more than one apostrophe (unlikely as it may be), it will take care of that as well. <br>
<br>
I assume that at the time you're running the query you don't know the name (O'Conner, O'Brian, etc.)-- in which case Roy's code will fit in perfectly.<br>
<br>
Regards,<br>
Tarek
 
Tarek,<br>
<br>
Actually Roy posted his reply while I was posting mine. I made a sub of my own to take a name apart if it detects an apostrophe so I guess in this case I reinvented roy's wheel<br>

 
For ASP programmers :<br>
IN VBScript you can also use the replace(string,&quot;'&quot;,&quot;''&quot;) command...<br>
<br>
Regards<br>
<p>Peter Van Eeckhoutte<br><a href=mailto:peter.ve@itinera.be>peter.ve@itinera.be</a><br><a href= > </a><br>
 
Some people are making this alot harder then it has to be. OK create a module. Put this code in it.



Public Function FindChr(strTemp As String) As String

FindChr = Replace(strTemp, &quot;'&quot;, Chr(34), 1)

End Function



Public Function GetBackChr(strTemp As String) As String

GetBackChr = Replace(strTemp, Chr(34), Chr(39))

End Function


The FindChr function will replace the '
the GetBackChr will get back the '

For example here I remove the ' for the SQL Query then set it back

txtDescription = modUtilities.FindChr(txtDescription)
m_objNewDrawing.sDescription = txtDescription
txtDescription = modUtilities.GetBackChr(txtDescription)
 
Have you tried doing a Like instead of an =
like ('O?Conner')
 
Hi everybody!
I was experiencing the same problem of a single quote in a query string. Although the problem is solved in my application by replacing ( ' ) character,But it has now created another problem in reporting point of view. I am using Crystal reports to prepare the reports. Now problem is that my reports get data from that table in which my application saves the data. so when i get a report the data is printed like
O&quot;Connor
instead of
O'Connor
And it is a lot of work to format every string in the report. So isnt there any way that i can save (') based string as it is using a query string. I can do that by using a recordset and then saving data using the recordset, but that method does not suite my application.
So if anybody has a real solution to that then plz do reply

Thanks in advance
 
Try this. It works fine if you have multiple ' or &quot; , but will blow up if your string has a # in it
&quot;select * from sometable where name = #O'conner#&quot; or
&quot;select * from sometable where name = #O'conner's#&quot;
Tim

Remember the KISS principle:
Keep It Simple, Stupid!
 
I have a nice solutions if using ADO and its low on processing power:

Code:
strSQL = &quot;SELECT * FROM tblMyTable WHERE myColumn = ?&quot;
cmMyCommandObject.CommandType = adText
cmMyCommandObject.CommandText = strSQL
cmMyCommandObject.Parameters.Append _
      MyCommandObject.CreateParameter(&quot;1&quot;, adVarChar, adParamInput, 20, myVar)
set rsResults = cmMyCommandObject.Execute


This will solve all possible xcharacter issues, and doesn't require a find and replace operation(s) or multiple string concatenation. A side effect is that you can also set
Code:
Prepared = True
and create a temporary stored procedure by just replacing parameters.

This model also works with Inserts/Updates/Deletes very nicely. It is a VERY flexible model if you are not using DB Stored Procedures.

James :)
James Culshaw
jculshaw@active-data-solutions.co.uk
 
hi
Thanks &quot;culshaja&quot; for the solution that u gave, It solved my problem and works fine for my problem.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top