Solved

SQL Query - Update Syntax issue with Apostrophe

Posted on 2009-04-09
7
489 Views
Last Modified: 2012-05-06
I am creating a small application for taking notes etc.  However, in my textbox when I use any Apostrophe, the program is giving me error.  

Incorrect syntax near 's'.  Unclosed quotation mark after the character string ')'.  

So any way to get around this?
0
Comment
Question by:holemania
7 Comments
 
LVL 11

Expert Comment

by:N R
ID: 24111135
us 2 '.  Like if it is

'This is Bob's House'

should be

'This is Bob''s House'
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24111203
how are you creating this sql?
if manually i.e. in code (not bounded forms) then wrap that field with double quotes

eg

dim sSql as string

sSql = "update mytable set myfield = " & chr$(34) & myvalue & chr$(34)

chr$(34) is basically double quotes that u wrap your variable in
0
 
LVL 9

Accepted Solution

by:
Sreedhar Vengala earned 250 total points
ID: 24111295
OK, so you're wanting your values to include the surrounding apostrophes in
the values stored on the database.  IN that case, you need to use *three*
apostrophes.  This is because you want to leave a single apostrophe to tell
the parser that the enclosed value is a string, but also include double
apostrophes to tell the parser to generate a single apostrophe after parsing
the SQL.  I.e.

Insert into table values('''fields''', '''morefields''');

Now this may cause problems, depending on how good the SQL parser you are
using is.  For instance, if it always generates a single apostrophe from the
first double apostrophe it finds, you may end up back where you were with
the two apostrophe example you gave.

There is another, simpler alternative of course.  Use double quotes instead
of apostrophes in your strings.  I.e.

Insert into table values('"fields"', '"morefields"');

It all depends on why you want the apostrophes to be stored in the fields
with the values.  If you're going to use them in processing later when the
data is read, then you can easily do a replace of the double quotes with
apostrophes after the values are read from the database.  Off my head, I
can't think why you would want to enclose values in apostrophes like this,
as the data type of the field should already know they are strings, so
storing them with the data seems a bit redundant.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 250 total points
ID: 24111632
Sorry, I thought I was in the msaccess zone, lol

but with sql, to wrap a string with single quotes, wrap it in double quotes. It sill applies except you would use   "  (shift 2)
0
 

Author Comment

by:holemania
ID: 24111707
Below is how my query looks like.  My richtextbox, I want the user to type in notes and sometimes when they using apostrophe in the note, it throw the error.

My snytax:
Dim strInsert As String = "INSERT INTO NOTE VALUES(" & strNoteID & ", " & txtID.TEXT & ", '" & rtbNote.TEXT & "')"


This part of the syntax '" & rtbNote.TEXT & "' is what is giving me the error.
0
 

Author Comment

by:holemania
ID: 24111853
Okay this seems fix the issue.

Dim strNote as String = Replace(rtbNote.Text, "'", "''")

Dim strInsert As String = "INSERT INTO NOTE VALUES(" & strNoteID & ", " & txtID.TEXT & ", '" & strNote & "')"
0
 

Author Closing Comment

by:holemania
ID: 31568720
Thanks for the help.  Not quite what I was looking for me steer me in the right direction.

Basically replacing ' with double '' seems to fix the issue.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question