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
Solved

Appending a text file with & and ' in the fields and double quots

Posted on 2012-12-27
3
301 Views
Last Modified: 2012-12-30
Hello.

When I execute the below, my rrecords append to the correct file - they all begin and end with double quotes:

DECLARE @var1 varchar(4000);
SELECT @var1 = 'echo "' + col1 + CHAR(9) + col2 + char(9) +  col3 + '" >>\\pathwhere\fileexists.txt' FROM Table1 WITH(NOLOCK);
EXEC master..xp_cmdshell @var1;

When I remove the double quotes, none of my records with ampersands and single quotes print - instead, this error is logged:  'fieldname' is not recognized as an internal or external command.

When I restore the double quotes, my fieldnames print but every row begins and ends with double quotes.

My customer can not have the double quote but still needs the data with ampersands and quotes in the tite field to print.  (ex  Micky & Minnie)

How can I either escape the double quotes or modify my statement so my titles with ampersands and single quotes append to my file?
0
Comment
Question by:programmher
3 Comments
 
LVL 9

Expert Comment

by:shorvath
ID: 38725807
sample data?
0
 
LVL 12

Accepted Solution

by:
sachitjain earned 150 total points
ID: 38725931
I guess issue is only with ampersand character and not with single quotes and that could be resolved by replacing each & character with ^&.

Like this:

DECLARE @var1 varchar(4000);
SELECT @var1 = 'echo ' + replace(col1, '&', '^&') + CHAR(9) + replace(col2, '&', '^&')
            + char(9) +  replace(col3, '&', '^&') + ' >>\\pathwhere\fileexists.txt' FROM Table1;
EXEC master..xp_cmdshell @var1;
0
 
LVL 12

Expert Comment

by:Saurabh Bhadauria
ID: 38726779
Optionally you can use SQLCMD ......


Create table  temp (Col1 nvarchar(20),col2 nvarchar(20)) 

insert into temp
select 'Saurabh & bhadauria' , 'Saurabh " bhadauria '

DECLARE @var1 varchar(4000);
SELECT @var1 = 'SqlCMD  -S.   -E   -h    -1  -q " set nocount on select col1 + CHAR(9) + col2  from   test_sa.dbo.temp ; "    -o D:\33.txt'
EXEC master..xp_cmdshell @var1; 

Open in new window


Here I have suppressed headers with  ( -h -1)  ..... and  row-count  summary with  Set NoCount ON ...

This will produced desired result...

look at below link for full details...
http://msdn.microsoft.com/en-us/library/ms165702(v=sql.105).aspx
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

829 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