473,414 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Double Quotes In Data?

Is his just a flat-out "No-No" or is there some workaround when it comes time
for SQL searches and DAO.FindFirsts against fields containing same?

I can see maybe wrapping the value searched for in single quotes to deal with
embedded double quotes, but if there are both embedded single AND double quotes,
I can't see any way to deal with it.

So, am I doing the right thing to remove all embedded double quotes from my data
and write validation routines to enforce same in the data entry screens?

Or is there some workaround?
--
PeteCresswell
Nov 13 '05 #1
4 13269
Try replacing each double-quote with a pair of double-quotes, e.g. ...

Edwin "Buzz" Aldrin

.... becomes ...

"Edwin ""Buzz"" Aldrin"

It has always worked for me.

Nov 13 '05 #2
The most thorough way is to wrap the string in a function that checks for
quote marks and adjusts accordingly.

Here's a function I use to wrap string that I'm passing to an SQL string
that will be used in a query to ensure that the quote marks are handled
properly. It's based on some code David Epsom posted here several years ago.
Sorry for the dearth of comments in the code.

The function looks inside the string for single or double quotes. If there's
only one kind or the other, it encloses it in the other kind (e.g. a string
with an apostrophe gets double quotes around it, a string with double quotes
gets single quotes around it). If there are *both* (imagine if Edwin "Buzz"
Aldrin were Edwin "Buzz" O'Hare), it replaces the quotes with the equivalent
CHR() functions.

Usage is as follows:

strSQL = "SELECT * FROM tblPeople WHERE LastName Like " & SQLQuote
(strString) & ";"

you can also have it add wildcards to modify the search by adding additional
boolean arguments:

for a wildcard before the string (search for "*endofname"):

strSQL = "SELECT * FROM tblPeople WHERE LastName Like " & SQLQuote
(strString, True) & ";"

for a wildcard after the string (search for "startofname*"):

strSQL = "SELECT * FROM tblPeople WHERE LastName Like " & SQLQuote
(strString, , True) & ";"

---------------Begin Code---------------

Const SingleQuote = "'"
Const DoubleQuote = """"
Const StarSingleQuote As String = "*'"
Const SingleQuoteStar As String = "'*"
Const DoubleQuoteStar As String = """" & "*"
Const StarDoubleQuote As String = "*" & """"
Const CHRDouble As String = "' & CHR(34) & '"
Const CHRSingle As String = "' & CHR(39) & '"

Public Function SQLQuote(ByVal strIn As String, _
Optional ByVal PrefixWildCard As Boolean = False, _
Optional ByVal SuffixWildcard As Boolean = False _
) As String

Dim blnSingleQuote As Boolean
Dim blnDoubleQuote As Boolean
Dim blnCHRStart As Boolean
Dim blnCHREnd As Boolean
Dim strCompare As String

blnDoubleQuote = InStr(strIn, DoubleQuote)

If blnDoubleQuote Then
' There are double quotes; check for single
' quotes as well

blnSingleQuote = InStr(strIn, SingleQuote)

If blnSingleQuote Then
' Both single AND double quotes -- complicated,
' replace with CHR functions

' Replace single quotes with CHR version
strCompare = Replace$(strIn, SingleQuote, CHRSingle)

' Clean up front and back
If Left$(strCompare, 15) = CHRSingle Then _
strCompare = Mid$(strCompare, 5)

If Right$(strCompare, 15) = CHRSingle Then _
strCompare = Left$(strCompare, Len(strCompare) - 4)

' Replace double quotes with CHR version
strCompare = Replace$(strCompare, DoubleQuote, CHRDouble)

' Clean up front and back
If Left$(strCompare, 15) = CHRDouble Then _
strCompare = Mid$(strCompare, 5)

If Right$(strCompare, 15) = CHRDouble Then _
strCompare = Left$(strCompare, Len(strCompare) - 4)

' String parsed, now check for wildcards

' First, check whether the string starts or ends with
' a CHR(n) value

blnCHREnd = (StrComp(Right$(strCompare, 8), " CHR(34)")) = 0 Or
_
(StrComp(Right$(strCompare, 8), " CHR(39)")) = 0

blnCHRStart = Left$(strCompare, 5) = "CHR(3"

' Add wildcard and start/end single quotes, as
' needed
'
If PrefixWildCard Then
If blnCHRStart Then
strCompare = "'*' & " & strCompare
Else
strCompare = SingleQuoteStar & strCompare
End If

ElseIf Not blnCHRStart Then
strCompare = SingleQuote & strCompare

End If

If SuffixWildcard Then

If blnCHREnd Then
strCompare = strCompare & " & '*'"
Else
strCompare = strCompare & StarSingleQuote
End If

ElseIf Not blnCHREnd Then
strCompare = strCompare & SingleQuote

End If

Else
' Only double quotes, so using single
' quotes around it is safe

If PrefixWildCard Then
strCompare = SingleQuoteStar & strIn
Else
strCompare = SingleQuote & strIn
End If

If SuffixWildcard Then
strCompare = strCompare & StarSingleQuote
Else
strCompare = strCompare & SingleQuote
End If
End If
Else
' No double quotes in the string
' so using double quotes around it
' is safe (even if there are single
' quotes in it).
'
If PrefixWildCard Then
strCompare = DoubleQuoteStar & strIn
Else
strCompare = DoubleQuote & strIn
End If

If SuffixWildcard Then
strCompare = strCompare & StarDoubleQuote
Else
strCompare = strCompare & DoubleQuote
End If

End If

SQLQuote = strCompare

End Function

----------------End Code-------------------
Nov 13 '05 #3
Hi, Bruce.
If there are *both* (imagine if Edwin "Buzz" Aldrin were Edwin "Buzz"
O'Hare), it replaces the quotes with the equivalent CHR() functions.


There's nothing wrong with doing that, but

"Edwin ""Buzz"" O'Hare"

is a valid string as-is.

Nov 13 '05 #4
Public Function S2SQL(ByVal inString As String) As String
Dim s As String
Dim i As Long
Do
i = InStr(inString, "'") ' double, single, double
If i = 0 Then Exit Do
s = s & Left$(inString, i - 1) & "''" ' d, s, s, d
inString = Mid$(inString, i + 1)
Loop
S2SQL = "'" & s & inString & "'" ' d, s, d - twice
End Function
--
Darryl Kerkeslager
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jakanapes | last post by:
Hi all, I'm looking for a way to scan a block of text and replace all the double quotes (") with single quotes ('). I'm using PHP to pull text out of a mySQL table and then feed the text into...
7
by: Leif B. Kristensen | last post by:
I'm working with a Python program to insert / update textual data into a PostgreSQL database. The text has single and double quotes in it, and I wonder: What is the easiest way to escape quotes in...
2
by: nurddin19 | last post by:
Hi, I have an Excel file (which is converted to csv by a tool and then uploaded to a MySQL database) and then php is used to display the data on the website. In the Excel file i have text...
2
by: girish | last post by:
In my XML document, some node attributes data contains both single quot and double quote characters, such as <input msg="Hello "World", What's up"/>. The double quotes are in form of escape...
24
by: deko | last post by:
I'm trying to log error messages and sometimes (no telling when or where) the message contains a string with double quotes. Is there a way get the query to insert the string with the double...
7
by: gar | last post by:
Hi, I need to replace all the double quotes (") in a textbox with single quotes ('). I used this code text= Replace(text, """", "'" This works fine (for normal double quotes).The problem...
1
by: desi.american | last post by:
I have a dynamically generates ASPX page with tables and data. Depending on user selection, the same page can be viewed as a simple web page (rendered in HTML) or as an excel spreadsheet. If the...
6
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello, I have some XML that is returned to my application from another vendor that I cannot change before it gets to me. I can only alter it after it gets to my application. That being said, I...
1
by: Yearwood | last post by:
Hi, I'm basically trying to import a CSV into an ACCESS database. Sample date is shown below: "",10173,"Development Manager - Social Economy Sector","Trust Bank",10153,,"Lolalll Pudd","Meet the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.