Solved

SQL Syntax

Posted on 2014-11-19
6
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 66

Accepted Solution

by:
Jim Horn earned 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

635 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