Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Syntax

Posted on 2014-11-19
6
Medium Priority
?
246 Views
Last Modified: 2014-11-20
I have a MS SQL table that holds data from several memo fields.  The data in the memo fields can contain apostrophes and double quotes.  How can I make it so that SQL accepts these?

Here is an example of hard coded values as an example:

insert into MDP_data (psc#,priority,fy,status_code,justification,staff_comments)
values
('1.0012','1','2016',
'B',
'Deferral of partial funding is recommended. Planning was approved in FY13 and partial funding was approved in FY14 and FY15. See worksheet for approved scope and funding details.',
'10/29 Comments: The LEA will bid in feb and start construction in April 2015.  DDs rec'd by DGS.  CDs are underway.  There maybe a delay in demolition.  There are several large alternates for this project.  MEMA issues are all worked out.  They would like to receive full funding for this project.')
0
Comment
Question by:mounty95
6 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 40453616
for single quotes within the text, replace each of them with 2 single quotes when you are inserting data from within sql server management studio. from application you don't have to worry about this.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 40453618
I don't think you need to worry about the double quotes
0
 

Author Comment

by:mounty95
ID: 40453742
My question is though, how do I replace the single quotes with a variable in an insert statement like the one in my example, but where instead of hard coded values, I have variable A, variable B, variable C, variable D, variable E, and variable F.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 40453777
>My question is though, how do I replace the single quotes with a variable in an insert statement like the one in my example,
If the source is a SQL table, then as Aneesh said replace single quotes with two single quotes.

Copy-paste this into your SSMS and execute to verify it works
-- Store a value with a single quote mark in a variable
Declare @str varchar(50) = 'O''Malley'

-- Stores in the variable as a single string
SELECT @str

-- Convert single quote marks to double quote marks
SELECT REPLACE(@str, char(39), char(39) + char(39)) 

Open in new window


Then wherever there's a sting, pass it using the above REPLACE function.

If your source is something other than SQL, that in itself won't work, as T-SQL won't know the difference between the single quotes surrounding a char value, and a single quote in a value like O'Malley in a variable.   So, that application will have to pass either two single-quote marks (per above code), or a 'dummy' value like pipe ( | ) which SQL will accept, then change to single quote marks again using REPLACE.
0
 
LVL 3

Expert Comment

by:Richard Obenchain
ID: 40454199
A better option would be to create a stored procedure to update these fields and use parameters passed from the code rather than executing a SQL statement directly.  That will also eliminate SQL injection problems down the line.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40454991
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

824 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