Solved

SQL Syntax

Posted on 2014-11-19
6
236 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Finding gaps or missing dates in a date range using TSQL 3 28
Isolation level setting TSQL View 10 30
Parse this column 6 27
MS SQL + date 6 24
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

821 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