Solved

SQL Syntax

Posted on 2014-11-19
6
230 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 65

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 65

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now