Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Syntax

Posted on 2014-11-19
6
Medium Priority
?
242 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

670 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