?
Solved

Problems with single quote character iwhen attempting to export data

Posted on 2010-09-15
10
Medium Priority
?
207 Views
Last Modified: 2012-05-10
Hi,

I am attempting to use the following query, which works fine when run in sql server management studio, to export data:

SELECT
            '1',
            isnull(left(DISTRICT,1),' '),
            isnull(left(CTV,2),'  '),
            isnull(left(CTV2,5),'     ')          
FROM [CensusData].[dbo].[MainQuestionnaire$]

The export command follows:

EXEC xp_cmdshell 'bcp "SELECT
            '1',
            isnull(left(DISTRICT,1),' '),
            isnull(left(CTV,2),'  '),
            isnull(left(CTV2,5),'     ')            
FROM [CensusData].[dbo].[MainQuestionnaire$]" queryout "C:\bcptest.txt" -T -c -t -S "MYSERVER"'

I know that the problem is with the single quote characters but don't know how to fix it.

Please help.
0
Comment
Question by:sib_user
[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
  • 5
  • 3
  • 2
10 Comments
 
LVL 15

Expert Comment

by:AmmarR
ID: 33679900
Dear you can use the replace command

check the code below

replace(column name,'''','')
EXEC xp_cmdshell 'bcp "SELECT 
            '1', 
            isnull(left(replace(DISTRICT,'''',''),1),' '),
            isnull(left(replace(CTV,'''',''),2),'  '),,
            isnull(left(replace(CTV2,'''','')),'     ')            
FROM [CensusData].[dbo].[MainQuestionnaire$]" queryout "C:\bcptest.txt" -T -c -t -S "MYSERVER"' 

Open in new window

0
 
LVL 8

Expert Comment

by:Knut Hunstad
ID: 33679905
From the documentation:

---
command_string cannot contain more than one set of double quotation marks. A single pair of quotation marks is necessary if any spaces are present in the file paths or program names referenced by command_string. If you have trouble with embedded spaces, consider using FAT 8.3 file names as a workaround
---

So the problem seems to be your double quotation marks, not the single ones!

Try this:

EXEC xp_cmdshell 'bcp "SELECT
            '1',
            isnull(left(DISTRICT,1),' '),
            isnull(left(CTV,2),'  '),
            isnull(left(CTV2,5),'     ')            
FROM [CensusData].[dbo].[MainQuestionnaire$]" queryout C:\bcptest.txt -T -c -t -S MYSERVER'


Please note this suggestion is just a guess and untested :-) But it should give you a clue where to look...

Good luck!
0
 

Author Comment

by:sib_user
ID: 33679986
AmmarR:

I get this error when I attempt your solution:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '),
            isnull(left(replace(CTV,'','),2),'.

Khun:

Your give the error:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '1'.
Which seems to refute your assertion that the double quotes is the problem.
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 15

Expert Comment

by:AmmarR
ID: 33680004
there were 2 commas at the end of the line

try this now
EXEC xp_cmdshell 'bcp "SELECT 
            '1', 
            isnull(left(replace(DISTRICT,'''',''),1),' '),
            isnull(left(replace(CTV,'''',''),2),'  '),
            isnull(left(replace(CTV2,'''','')),'     ')            
FROM [CensusData].[dbo].[MainQuestionnaire$]" queryout "C:\bcptest.txt" -T -c -t -S "MYSERVER"' 

Open in new window

0
 
LVL 8

Expert Comment

by:Knut Hunstad
ID: 33680036
I would start with going to a cmd-shell and try typing (or rather put it into a bat-file so you don't have to retype if it doesn't work):

bcp "SELECT
            '1',
            isnull(left(DISTRICT,1),' '),
            isnull(left(CTV,2),'  '),
            isnull(left(CTV2,5),'     ')            
FROM [CensusData].[dbo].[MainQuestionnaire$]" queryout "C:\bcptest.txt" -T -c -t -S "MYSERVER"

to check that works. Then:

bcp "SELECT
            '1',
            isnull(left(DISTRICT,1),' '),
            isnull(left(CTV,2),'  '),
            isnull(left(CTV2,5),'     ')            
FROM [CensusData].[dbo].[MainQuestionnaire$]" queryout C:\bcptest.txt -T -c -t -S MYSERVER

Should help you detect what the problem really is...
0
 

Author Comment

by:sib_user
ID: 33680180
AmmarR;
Same error.

khun:
Have a look at the attached image.
errors.bmp
0
 
LVL 8

Expert Comment

by:Knut Hunstad
ID: 33680242
You need to have it all on on line. I should have mentioned that, of course, sorry. Right now you sent one command:

bcp "SELECT

which triggered the explanation of the bcp command. Then you sent another command:

'1',

which was sort of difficult to understand :-) Etc. etc.

Put this into a text file you call TestSQL.bat:

bcp "SELECT '1', isnull(left(DISTRICT,1),' '), isnull(left(CTV,2),'  '), isnull(left(CTV2,5),'     ') FROM [CensusData].[dbo].[MainQuestionnaire$]" queryout "C:\bcptest.txt" -T -c -t -S "MYSERVER"

Then just write TestSQL.bat at the command prompt. Then try the same with:

bcp "SELECT '1', isnull(left(DISTRICT,1),' '), isnull(left(CTV,2),'  '), isnull(left(CTV2,5),'     ') FROM [CensusData].[dbo].[MainQuestionnaire$]" queryout C:\bcptest.txt -T -c -t -S MYSERVER
0
 

Author Comment

by:sib_user
ID: 33683484
khun,

Tried that and for some strange reason the server seems to be inaccessible when I run the batch file.

I can't figure it out because it runs well from sql server management studio.
0
 

Accepted Solution

by:
sib_user earned 0 total points
ID: 33683784
Got the answer here: ttp://social.msdn.microsoft.com/Forums/en/transactsql/thread/ff79934d-36c6-4078-8081-5e1f14df82c2

All I had to do was put two single quotes everywhere on quote appeared and make sure that everything is on thesame line.

Thanks very much for your attempts.
0
 

Author Closing Comment

by:sib_user
ID: 34186369
I got the answer on another forum.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

719 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