How can I output more than 256 characters to a file?SSIS SQL Result to tab delimited txt fileHow to output...
Can I Retrieve Email Addresses from BCC?
How does a character multiclassing into warlock get a focus?
Have I saved too much for retirement so far?
Applicability of Single Responsibility Principle
Mapping a list into a phase plot
What's the purpose of "true" in bash "if sudo true; then"
Where in the Bible does the greeting ("Dominus Vobiscum") used at Mass come from?
Was the picture area of a CRT a parallelogram (instead of a true rectangle)?
Is there a good way to store credentials outside of a password manager?
How can I use the arrow sign in my bash prompt?
What is difference between behavior and behaviour
Greatest common substring
Why are on-board computers allowed to change controls without notifying the pilots?
Efficiently merge handle parallel feature branches in SFDX
What would happen if the UK refused to take part in EU Parliamentary elections?
What is the term when two people sing in harmony, but they aren't singing the same notes?
What defines a dissertation?
Ways to speed up user implemented RK4
Hide Select Output from T-SQL
Is there a problem with hiding "forgot password" until it's needed?
Is exact Kanji stroke length important?
How does it work when somebody invests in my business?
Teaching indefinite integrals that require special-casing
Opposite of a diet
How can I output more than 256 characters to a file?
SSIS SQL Result to tab delimited txt fileHow to output more than 4000 characters in sqlcmdHow to avoid delimiter character in BCP export?Howto avoid textdata of trace gets truncated?SQLCMD does not prints Rows affected in log fileNot displaying special characters imported from a fileSelect number of times 2 specific values show up for one record idNvarchar column getting truncated to 256 charactersHow To Import Binary Dump File To SQL ServerCannot resolve the collation conflict between “SQL_Latin1_General_CP1_CI_AS” and “Latin1_General_CI_AI”
I am using the following command to write the output from a stored procedure to a file for further processing:
DECLARE @shellCommand VARCHAR(2000)
SET @shellCommand = 'SQLCMD -S ' + @@SERVERNAME + ' -d ' + @dbName + ' -U ' + @UserName + ' -P ' + @Password + ' -Q "' + @sqlCommand + '" -s "," -h -1 -k 1 -W -o "' + @outputfileName + '"'
@sqlCommand
is set correctly, to "EXEC StoredProcedureName parameterValue" and when run independently apparently produces the correct data.
The problem I'm having is that one of the fields contains text greater than 256 characters long and is getting truncated when fed through the SQLCMD command.
On reading the documentation I see that the default column width is 256 characters. So I'm looking at using the -y
or -Y
option to allow more than 256 characters to be output. However, these options aren't compatible with the -W
option which I was using to remove trailing spaces from the output. I started by using -y 0
(despite the performance warning - once I get the output working I can look at the performance). However, that produces some strange results.
The file consists of a header row followed by the data rows and should look something like this:
First,ABC,Number,String,ReallyLongString,...
A,0123,14.99,"Short string","Longish string",...
B,0456,23.99,"Normal string","Really, really long string that's causing problems",...
With the -W
option the file format is correct but the really long string is truncated. The reason we found it was because the trailing "
was missing and the file wasn't being read properly.
With -y 0
the really long string is getting output but large numbers of spaces are being added to apparently random columns. We're getting something like this:
First,ABC ,Number [...] ,String,ReallyLongString,...
A ,0123,14.99 [...] ,"Short string","Longish string",...
B ,0456,23.99 [...] ,"Normal string","Really, really long string that's causing problems",...
(There are many, many more spaces between "Number" and the next column as represented by the "[...]", I'm just showing a few).
There are more numeric values that have to be formatted in a similar way and it does appear that it's these that are causing the extra spaces after the value and before the next comma. We can live with a few extra spaces, but not as many as this as the resultant file is too large to be read by the target program.
The data is generated by a stored procedure that looks like this:
SELECT
'First' AS First,
'ABC' AS ABC,
'Number' AS Number,
'String' AS String,
'ReallyLongString' AS ReallyLongString,
...
UNION ALL
SELECT
'A' as First,
Column1 as ABC,
REPLACE(FORMAT(Column2, 'N2', 'en-GB'), ',', '') as Number, -- to get the format correct
'"' + Column3 + '"' as String,
'"' + Column4 + '"' as ReallyLongString,
....
FROM Table
WHERE <condition>
I'm thinking that the problem is the output from the stored procedure. I only added the -W
option to the SQLCMD because of the extra trailing spaces being output, but looking at the stored procedure I can't work out where they are coming from. I changed the number formatting to include RTRIM
:
RTRIM(REPLACE(FORMAT(Column2, 'N2', 'en-GB'), ',', ''))
but that appeared to make no difference.
Is there something I can do to the stored procedure or is there a combination of options to SQLCMD that will produce the desired output? Or am I going to have to find some other way of producing this file?
sql-server csv sqlcmd
|
show 5 more comments
I am using the following command to write the output from a stored procedure to a file for further processing:
DECLARE @shellCommand VARCHAR(2000)
SET @shellCommand = 'SQLCMD -S ' + @@SERVERNAME + ' -d ' + @dbName + ' -U ' + @UserName + ' -P ' + @Password + ' -Q "' + @sqlCommand + '" -s "," -h -1 -k 1 -W -o "' + @outputfileName + '"'
@sqlCommand
is set correctly, to "EXEC StoredProcedureName parameterValue" and when run independently apparently produces the correct data.
The problem I'm having is that one of the fields contains text greater than 256 characters long and is getting truncated when fed through the SQLCMD command.
On reading the documentation I see that the default column width is 256 characters. So I'm looking at using the -y
or -Y
option to allow more than 256 characters to be output. However, these options aren't compatible with the -W
option which I was using to remove trailing spaces from the output. I started by using -y 0
(despite the performance warning - once I get the output working I can look at the performance). However, that produces some strange results.
The file consists of a header row followed by the data rows and should look something like this:
First,ABC,Number,String,ReallyLongString,...
A,0123,14.99,"Short string","Longish string",...
B,0456,23.99,"Normal string","Really, really long string that's causing problems",...
With the -W
option the file format is correct but the really long string is truncated. The reason we found it was because the trailing "
was missing and the file wasn't being read properly.
With -y 0
the really long string is getting output but large numbers of spaces are being added to apparently random columns. We're getting something like this:
First,ABC ,Number [...] ,String,ReallyLongString,...
A ,0123,14.99 [...] ,"Short string","Longish string",...
B ,0456,23.99 [...] ,"Normal string","Really, really long string that's causing problems",...
(There are many, many more spaces between "Number" and the next column as represented by the "[...]", I'm just showing a few).
There are more numeric values that have to be formatted in a similar way and it does appear that it's these that are causing the extra spaces after the value and before the next comma. We can live with a few extra spaces, but not as many as this as the resultant file is too large to be read by the target program.
The data is generated by a stored procedure that looks like this:
SELECT
'First' AS First,
'ABC' AS ABC,
'Number' AS Number,
'String' AS String,
'ReallyLongString' AS ReallyLongString,
...
UNION ALL
SELECT
'A' as First,
Column1 as ABC,
REPLACE(FORMAT(Column2, 'N2', 'en-GB'), ',', '') as Number, -- to get the format correct
'"' + Column3 + '"' as String,
'"' + Column4 + '"' as ReallyLongString,
....
FROM Table
WHERE <condition>
I'm thinking that the problem is the output from the stored procedure. I only added the -W
option to the SQLCMD because of the extra trailing spaces being output, but looking at the stored procedure I can't work out where they are coming from. I changed the number formatting to include RTRIM
:
RTRIM(REPLACE(FORMAT(Column2, 'N2', 'en-GB'), ',', ''))
but that appeared to make no difference.
Is there something I can do to the stored procedure or is there a combination of options to SQLCMD that will produce the desired output? Or am I going to have to find some other way of producing this file?
sql-server csv sqlcmd
Where is the truncation actually happening? I don't think it is happening at character 257 given that the following works with 4001 characters:sqlcmd -Q "SELECT 'header' AS [tt] UNION ALL SELECT REPLICATE('O', 4000)+'x' AS [some_string];" -W -s "," -h -1 -k 1 -o "SQLCMD_long_line.txt"
.
– Solomon Rutzky
Feb 24 '17 at 15:29
@srutzky - The truncation appears to be happening when the file is written. If I run the stored procedure that generates the data I get the"Really long text...with ending"
correctly in the output window. But when we look in the file it's"Really long text... wit
, where thet
is the 256th character including the opening"
. Maybe it's an issue with the fact that I've had to quote the strings.
– ChrisF
Feb 24 '17 at 15:34
I don't think it has to do with quoting the strings as I just put my SELECT into a global temp proc, added the quotes like you have, and it still saves correctly to the output file. Can you try wrapping the "long line" in aCONVERT(VARCHAR(MAX), '"' + Column4 + '"') AS...
to see if that helps? Also, what version of SQLCMD are you using? I am using 11.0.2100.60.
– Solomon Rutzky
Feb 24 '17 at 15:42
@srutzky - Nope. Still truncated. Version 10.50.4042.0
– ChrisF
Feb 24 '17 at 15:51
You are running SQL Server 2012 (I assume at least this given the use ofFORMAT
) yet the 2008 R2 version of SQLCMD? Any chance of finding (or even downloading) a newer version of SQLCMD?
– Solomon Rutzky
Feb 24 '17 at 15:57
|
show 5 more comments
I am using the following command to write the output from a stored procedure to a file for further processing:
DECLARE @shellCommand VARCHAR(2000)
SET @shellCommand = 'SQLCMD -S ' + @@SERVERNAME + ' -d ' + @dbName + ' -U ' + @UserName + ' -P ' + @Password + ' -Q "' + @sqlCommand + '" -s "," -h -1 -k 1 -W -o "' + @outputfileName + '"'
@sqlCommand
is set correctly, to "EXEC StoredProcedureName parameterValue" and when run independently apparently produces the correct data.
The problem I'm having is that one of the fields contains text greater than 256 characters long and is getting truncated when fed through the SQLCMD command.
On reading the documentation I see that the default column width is 256 characters. So I'm looking at using the -y
or -Y
option to allow more than 256 characters to be output. However, these options aren't compatible with the -W
option which I was using to remove trailing spaces from the output. I started by using -y 0
(despite the performance warning - once I get the output working I can look at the performance). However, that produces some strange results.
The file consists of a header row followed by the data rows and should look something like this:
First,ABC,Number,String,ReallyLongString,...
A,0123,14.99,"Short string","Longish string",...
B,0456,23.99,"Normal string","Really, really long string that's causing problems",...
With the -W
option the file format is correct but the really long string is truncated. The reason we found it was because the trailing "
was missing and the file wasn't being read properly.
With -y 0
the really long string is getting output but large numbers of spaces are being added to apparently random columns. We're getting something like this:
First,ABC ,Number [...] ,String,ReallyLongString,...
A ,0123,14.99 [...] ,"Short string","Longish string",...
B ,0456,23.99 [...] ,"Normal string","Really, really long string that's causing problems",...
(There are many, many more spaces between "Number" and the next column as represented by the "[...]", I'm just showing a few).
There are more numeric values that have to be formatted in a similar way and it does appear that it's these that are causing the extra spaces after the value and before the next comma. We can live with a few extra spaces, but not as many as this as the resultant file is too large to be read by the target program.
The data is generated by a stored procedure that looks like this:
SELECT
'First' AS First,
'ABC' AS ABC,
'Number' AS Number,
'String' AS String,
'ReallyLongString' AS ReallyLongString,
...
UNION ALL
SELECT
'A' as First,
Column1 as ABC,
REPLACE(FORMAT(Column2, 'N2', 'en-GB'), ',', '') as Number, -- to get the format correct
'"' + Column3 + '"' as String,
'"' + Column4 + '"' as ReallyLongString,
....
FROM Table
WHERE <condition>
I'm thinking that the problem is the output from the stored procedure. I only added the -W
option to the SQLCMD because of the extra trailing spaces being output, but looking at the stored procedure I can't work out where they are coming from. I changed the number formatting to include RTRIM
:
RTRIM(REPLACE(FORMAT(Column2, 'N2', 'en-GB'), ',', ''))
but that appeared to make no difference.
Is there something I can do to the stored procedure or is there a combination of options to SQLCMD that will produce the desired output? Or am I going to have to find some other way of producing this file?
sql-server csv sqlcmd
I am using the following command to write the output from a stored procedure to a file for further processing:
DECLARE @shellCommand VARCHAR(2000)
SET @shellCommand = 'SQLCMD -S ' + @@SERVERNAME + ' -d ' + @dbName + ' -U ' + @UserName + ' -P ' + @Password + ' -Q "' + @sqlCommand + '" -s "," -h -1 -k 1 -W -o "' + @outputfileName + '"'
@sqlCommand
is set correctly, to "EXEC StoredProcedureName parameterValue" and when run independently apparently produces the correct data.
The problem I'm having is that one of the fields contains text greater than 256 characters long and is getting truncated when fed through the SQLCMD command.
On reading the documentation I see that the default column width is 256 characters. So I'm looking at using the -y
or -Y
option to allow more than 256 characters to be output. However, these options aren't compatible with the -W
option which I was using to remove trailing spaces from the output. I started by using -y 0
(despite the performance warning - once I get the output working I can look at the performance). However, that produces some strange results.
The file consists of a header row followed by the data rows and should look something like this:
First,ABC,Number,String,ReallyLongString,...
A,0123,14.99,"Short string","Longish string",...
B,0456,23.99,"Normal string","Really, really long string that's causing problems",...
With the -W
option the file format is correct but the really long string is truncated. The reason we found it was because the trailing "
was missing and the file wasn't being read properly.
With -y 0
the really long string is getting output but large numbers of spaces are being added to apparently random columns. We're getting something like this:
First,ABC ,Number [...] ,String,ReallyLongString,...
A ,0123,14.99 [...] ,"Short string","Longish string",...
B ,0456,23.99 [...] ,"Normal string","Really, really long string that's causing problems",...
(There are many, many more spaces between "Number" and the next column as represented by the "[...]", I'm just showing a few).
There are more numeric values that have to be formatted in a similar way and it does appear that it's these that are causing the extra spaces after the value and before the next comma. We can live with a few extra spaces, but not as many as this as the resultant file is too large to be read by the target program.
The data is generated by a stored procedure that looks like this:
SELECT
'First' AS First,
'ABC' AS ABC,
'Number' AS Number,
'String' AS String,
'ReallyLongString' AS ReallyLongString,
...
UNION ALL
SELECT
'A' as First,
Column1 as ABC,
REPLACE(FORMAT(Column2, 'N2', 'en-GB'), ',', '') as Number, -- to get the format correct
'"' + Column3 + '"' as String,
'"' + Column4 + '"' as ReallyLongString,
....
FROM Table
WHERE <condition>
I'm thinking that the problem is the output from the stored procedure. I only added the -W
option to the SQLCMD because of the extra trailing spaces being output, but looking at the stored procedure I can't work out where they are coming from. I changed the number formatting to include RTRIM
:
RTRIM(REPLACE(FORMAT(Column2, 'N2', 'en-GB'), ',', ''))
but that appeared to make no difference.
Is there something I can do to the stored procedure or is there a combination of options to SQLCMD that will produce the desired output? Or am I going to have to find some other way of producing this file?
sql-server csv sqlcmd
sql-server csv sqlcmd
edited Feb 24 '17 at 14:48
ChrisF
asked Feb 24 '17 at 14:21
ChrisFChrisF
215215
215215
Where is the truncation actually happening? I don't think it is happening at character 257 given that the following works with 4001 characters:sqlcmd -Q "SELECT 'header' AS [tt] UNION ALL SELECT REPLICATE('O', 4000)+'x' AS [some_string];" -W -s "," -h -1 -k 1 -o "SQLCMD_long_line.txt"
.
– Solomon Rutzky
Feb 24 '17 at 15:29
@srutzky - The truncation appears to be happening when the file is written. If I run the stored procedure that generates the data I get the"Really long text...with ending"
correctly in the output window. But when we look in the file it's"Really long text... wit
, where thet
is the 256th character including the opening"
. Maybe it's an issue with the fact that I've had to quote the strings.
– ChrisF
Feb 24 '17 at 15:34
I don't think it has to do with quoting the strings as I just put my SELECT into a global temp proc, added the quotes like you have, and it still saves correctly to the output file. Can you try wrapping the "long line" in aCONVERT(VARCHAR(MAX), '"' + Column4 + '"') AS...
to see if that helps? Also, what version of SQLCMD are you using? I am using 11.0.2100.60.
– Solomon Rutzky
Feb 24 '17 at 15:42
@srutzky - Nope. Still truncated. Version 10.50.4042.0
– ChrisF
Feb 24 '17 at 15:51
You are running SQL Server 2012 (I assume at least this given the use ofFORMAT
) yet the 2008 R2 version of SQLCMD? Any chance of finding (or even downloading) a newer version of SQLCMD?
– Solomon Rutzky
Feb 24 '17 at 15:57
|
show 5 more comments
Where is the truncation actually happening? I don't think it is happening at character 257 given that the following works with 4001 characters:sqlcmd -Q "SELECT 'header' AS [tt] UNION ALL SELECT REPLICATE('O', 4000)+'x' AS [some_string];" -W -s "," -h -1 -k 1 -o "SQLCMD_long_line.txt"
.
– Solomon Rutzky
Feb 24 '17 at 15:29
@srutzky - The truncation appears to be happening when the file is written. If I run the stored procedure that generates the data I get the"Really long text...with ending"
correctly in the output window. But when we look in the file it's"Really long text... wit
, where thet
is the 256th character including the opening"
. Maybe it's an issue with the fact that I've had to quote the strings.
– ChrisF
Feb 24 '17 at 15:34
I don't think it has to do with quoting the strings as I just put my SELECT into a global temp proc, added the quotes like you have, and it still saves correctly to the output file. Can you try wrapping the "long line" in aCONVERT(VARCHAR(MAX), '"' + Column4 + '"') AS...
to see if that helps? Also, what version of SQLCMD are you using? I am using 11.0.2100.60.
– Solomon Rutzky
Feb 24 '17 at 15:42
@srutzky - Nope. Still truncated. Version 10.50.4042.0
– ChrisF
Feb 24 '17 at 15:51
You are running SQL Server 2012 (I assume at least this given the use ofFORMAT
) yet the 2008 R2 version of SQLCMD? Any chance of finding (or even downloading) a newer version of SQLCMD?
– Solomon Rutzky
Feb 24 '17 at 15:57
Where is the truncation actually happening? I don't think it is happening at character 257 given that the following works with 4001 characters:
sqlcmd -Q "SELECT 'header' AS [tt] UNION ALL SELECT REPLICATE('O', 4000)+'x' AS [some_string];" -W -s "," -h -1 -k 1 -o "SQLCMD_long_line.txt"
.– Solomon Rutzky
Feb 24 '17 at 15:29
Where is the truncation actually happening? I don't think it is happening at character 257 given that the following works with 4001 characters:
sqlcmd -Q "SELECT 'header' AS [tt] UNION ALL SELECT REPLICATE('O', 4000)+'x' AS [some_string];" -W -s "," -h -1 -k 1 -o "SQLCMD_long_line.txt"
.– Solomon Rutzky
Feb 24 '17 at 15:29
@srutzky - The truncation appears to be happening when the file is written. If I run the stored procedure that generates the data I get the
"Really long text...with ending"
correctly in the output window. But when we look in the file it's "Really long text... wit
, where the t
is the 256th character including the opening "
. Maybe it's an issue with the fact that I've had to quote the strings.– ChrisF
Feb 24 '17 at 15:34
@srutzky - The truncation appears to be happening when the file is written. If I run the stored procedure that generates the data I get the
"Really long text...with ending"
correctly in the output window. But when we look in the file it's "Really long text... wit
, where the t
is the 256th character including the opening "
. Maybe it's an issue with the fact that I've had to quote the strings.– ChrisF
Feb 24 '17 at 15:34
I don't think it has to do with quoting the strings as I just put my SELECT into a global temp proc, added the quotes like you have, and it still saves correctly to the output file. Can you try wrapping the "long line" in a
CONVERT(VARCHAR(MAX), '"' + Column4 + '"') AS...
to see if that helps? Also, what version of SQLCMD are you using? I am using 11.0.2100.60.– Solomon Rutzky
Feb 24 '17 at 15:42
I don't think it has to do with quoting the strings as I just put my SELECT into a global temp proc, added the quotes like you have, and it still saves correctly to the output file. Can you try wrapping the "long line" in a
CONVERT(VARCHAR(MAX), '"' + Column4 + '"') AS...
to see if that helps? Also, what version of SQLCMD are you using? I am using 11.0.2100.60.– Solomon Rutzky
Feb 24 '17 at 15:42
@srutzky - Nope. Still truncated. Version 10.50.4042.0
– ChrisF
Feb 24 '17 at 15:51
@srutzky - Nope. Still truncated. Version 10.50.4042.0
– ChrisF
Feb 24 '17 at 15:51
You are running SQL Server 2012 (I assume at least this given the use of
FORMAT
) yet the 2008 R2 version of SQLCMD? Any chance of finding (or even downloading) a newer version of SQLCMD?– Solomon Rutzky
Feb 24 '17 at 15:57
You are running SQL Server 2012 (I assume at least this given the use of
FORMAT
) yet the 2008 R2 version of SQLCMD? Any chance of finding (or even downloading) a newer version of SQLCMD?– Solomon Rutzky
Feb 24 '17 at 15:57
|
show 5 more comments
2 Answers
2
active
oldest
votes
While trying out the various suggestions in the comments I have stumbled across a solution.
I replaced:
'"' + Column4 + '"' as ReallyLongString,
with:
'"' + REPLICATE('O', 4000) + '"' AS ReallyLongString,
and all of the text was output.
However, using a variable:
DECLARE @longText NVARCHAR(MAX)
SET @longText = REPLICATE('O', 4000);
'"' + @longText + '"' AS ReallyLongString,
didn't.
What did work was declaring the variable with a specific length:
DECLARE @longText NVARCHAR(4000)
So, as the text in this case is never going to be longer than 2000 characters (the device reading the file can't cope with text that long) I convert all the text to 2000 character long strings:
SELECT
'A' as First,
Column1 as ABC,
REPLACE(FORMAT(Column2, 'N2', 'en-GB'), ',', '') as Number, -- to get the format correct
'"' + CAST(Column3 as NVARCHAR(2000)) + '"' as String,
'"' + CAST(Column4 as NVARCHAR(2000)) + '"' as ReallyLongString,
....
FROM Table
WHERE <condition>
This is the how. I still don't really know the why.
add a comment |
Strange... using -y 0
actually works perfectly for me. No white space padding or any other formatting issues. It actually works better than -W
, even though the documentation doesn't explain why and still recommends using -W
:
-W
This option removes trailing spaces from a column. Use this option together with the -s option when preparing data that is to be exported to another application. Cannot be used with the -y or -Y options.
It seems to me like -y 0
actually does what -W
advertises, and more (unlimited string length). The only other option I had to use was -s ","
.
I'm using version 13.1.0007.0
on Linux, so maybe the OP's issue is something that's been fixed in higher versions.
The only issue I encountered was a non-printable character (the box looking thing) being added at the end of very long strings (over 100K chars), but when the strings are quoted ('"' + field + '"'
), the character goes away. ¯_(ツ)_/¯
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f165429%2fhow-can-i-output-more-than-256-characters-to-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
While trying out the various suggestions in the comments I have stumbled across a solution.
I replaced:
'"' + Column4 + '"' as ReallyLongString,
with:
'"' + REPLICATE('O', 4000) + '"' AS ReallyLongString,
and all of the text was output.
However, using a variable:
DECLARE @longText NVARCHAR(MAX)
SET @longText = REPLICATE('O', 4000);
'"' + @longText + '"' AS ReallyLongString,
didn't.
What did work was declaring the variable with a specific length:
DECLARE @longText NVARCHAR(4000)
So, as the text in this case is never going to be longer than 2000 characters (the device reading the file can't cope with text that long) I convert all the text to 2000 character long strings:
SELECT
'A' as First,
Column1 as ABC,
REPLACE(FORMAT(Column2, 'N2', 'en-GB'), ',', '') as Number, -- to get the format correct
'"' + CAST(Column3 as NVARCHAR(2000)) + '"' as String,
'"' + CAST(Column4 as NVARCHAR(2000)) + '"' as ReallyLongString,
....
FROM Table
WHERE <condition>
This is the how. I still don't really know the why.
add a comment |
While trying out the various suggestions in the comments I have stumbled across a solution.
I replaced:
'"' + Column4 + '"' as ReallyLongString,
with:
'"' + REPLICATE('O', 4000) + '"' AS ReallyLongString,
and all of the text was output.
However, using a variable:
DECLARE @longText NVARCHAR(MAX)
SET @longText = REPLICATE('O', 4000);
'"' + @longText + '"' AS ReallyLongString,
didn't.
What did work was declaring the variable with a specific length:
DECLARE @longText NVARCHAR(4000)
So, as the text in this case is never going to be longer than 2000 characters (the device reading the file can't cope with text that long) I convert all the text to 2000 character long strings:
SELECT
'A' as First,
Column1 as ABC,
REPLACE(FORMAT(Column2, 'N2', 'en-GB'), ',', '') as Number, -- to get the format correct
'"' + CAST(Column3 as NVARCHAR(2000)) + '"' as String,
'"' + CAST(Column4 as NVARCHAR(2000)) + '"' as ReallyLongString,
....
FROM Table
WHERE <condition>
This is the how. I still don't really know the why.
add a comment |
While trying out the various suggestions in the comments I have stumbled across a solution.
I replaced:
'"' + Column4 + '"' as ReallyLongString,
with:
'"' + REPLICATE('O', 4000) + '"' AS ReallyLongString,
and all of the text was output.
However, using a variable:
DECLARE @longText NVARCHAR(MAX)
SET @longText = REPLICATE('O', 4000);
'"' + @longText + '"' AS ReallyLongString,
didn't.
What did work was declaring the variable with a specific length:
DECLARE @longText NVARCHAR(4000)
So, as the text in this case is never going to be longer than 2000 characters (the device reading the file can't cope with text that long) I convert all the text to 2000 character long strings:
SELECT
'A' as First,
Column1 as ABC,
REPLACE(FORMAT(Column2, 'N2', 'en-GB'), ',', '') as Number, -- to get the format correct
'"' + CAST(Column3 as NVARCHAR(2000)) + '"' as String,
'"' + CAST(Column4 as NVARCHAR(2000)) + '"' as ReallyLongString,
....
FROM Table
WHERE <condition>
This is the how. I still don't really know the why.
While trying out the various suggestions in the comments I have stumbled across a solution.
I replaced:
'"' + Column4 + '"' as ReallyLongString,
with:
'"' + REPLICATE('O', 4000) + '"' AS ReallyLongString,
and all of the text was output.
However, using a variable:
DECLARE @longText NVARCHAR(MAX)
SET @longText = REPLICATE('O', 4000);
'"' + @longText + '"' AS ReallyLongString,
didn't.
What did work was declaring the variable with a specific length:
DECLARE @longText NVARCHAR(4000)
So, as the text in this case is never going to be longer than 2000 characters (the device reading the file can't cope with text that long) I convert all the text to 2000 character long strings:
SELECT
'A' as First,
Column1 as ABC,
REPLACE(FORMAT(Column2, 'N2', 'en-GB'), ',', '') as Number, -- to get the format correct
'"' + CAST(Column3 as NVARCHAR(2000)) + '"' as String,
'"' + CAST(Column4 as NVARCHAR(2000)) + '"' as ReallyLongString,
....
FROM Table
WHERE <condition>
This is the how. I still don't really know the why.
answered Mar 7 '17 at 10:53
ChrisFChrisF
215215
215215
add a comment |
add a comment |
Strange... using -y 0
actually works perfectly for me. No white space padding or any other formatting issues. It actually works better than -W
, even though the documentation doesn't explain why and still recommends using -W
:
-W
This option removes trailing spaces from a column. Use this option together with the -s option when preparing data that is to be exported to another application. Cannot be used with the -y or -Y options.
It seems to me like -y 0
actually does what -W
advertises, and more (unlimited string length). The only other option I had to use was -s ","
.
I'm using version 13.1.0007.0
on Linux, so maybe the OP's issue is something that's been fixed in higher versions.
The only issue I encountered was a non-printable character (the box looking thing) being added at the end of very long strings (over 100K chars), but when the strings are quoted ('"' + field + '"'
), the character goes away. ¯_(ツ)_/¯
add a comment |
Strange... using -y 0
actually works perfectly for me. No white space padding or any other formatting issues. It actually works better than -W
, even though the documentation doesn't explain why and still recommends using -W
:
-W
This option removes trailing spaces from a column. Use this option together with the -s option when preparing data that is to be exported to another application. Cannot be used with the -y or -Y options.
It seems to me like -y 0
actually does what -W
advertises, and more (unlimited string length). The only other option I had to use was -s ","
.
I'm using version 13.1.0007.0
on Linux, so maybe the OP's issue is something that's been fixed in higher versions.
The only issue I encountered was a non-printable character (the box looking thing) being added at the end of very long strings (over 100K chars), but when the strings are quoted ('"' + field + '"'
), the character goes away. ¯_(ツ)_/¯
add a comment |
Strange... using -y 0
actually works perfectly for me. No white space padding or any other formatting issues. It actually works better than -W
, even though the documentation doesn't explain why and still recommends using -W
:
-W
This option removes trailing spaces from a column. Use this option together with the -s option when preparing data that is to be exported to another application. Cannot be used with the -y or -Y options.
It seems to me like -y 0
actually does what -W
advertises, and more (unlimited string length). The only other option I had to use was -s ","
.
I'm using version 13.1.0007.0
on Linux, so maybe the OP's issue is something that's been fixed in higher versions.
The only issue I encountered was a non-printable character (the box looking thing) being added at the end of very long strings (over 100K chars), but when the strings are quoted ('"' + field + '"'
), the character goes away. ¯_(ツ)_/¯
Strange... using -y 0
actually works perfectly for me. No white space padding or any other formatting issues. It actually works better than -W
, even though the documentation doesn't explain why and still recommends using -W
:
-W
This option removes trailing spaces from a column. Use this option together with the -s option when preparing data that is to be exported to another application. Cannot be used with the -y or -Y options.
It seems to me like -y 0
actually does what -W
advertises, and more (unlimited string length). The only other option I had to use was -s ","
.
I'm using version 13.1.0007.0
on Linux, so maybe the OP's issue is something that's been fixed in higher versions.
The only issue I encountered was a non-printable character (the box looking thing) being added at the end of very long strings (over 100K chars), but when the strings are quoted ('"' + field + '"'
), the character goes away. ¯_(ツ)_/¯
answered 12 mins ago
Marco RoyMarco Roy
143115
143115
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f165429%2fhow-can-i-output-more-than-256-characters-to-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Where is the truncation actually happening? I don't think it is happening at character 257 given that the following works with 4001 characters:
sqlcmd -Q "SELECT 'header' AS [tt] UNION ALL SELECT REPLICATE('O', 4000)+'x' AS [some_string];" -W -s "," -h -1 -k 1 -o "SQLCMD_long_line.txt"
.– Solomon Rutzky
Feb 24 '17 at 15:29
@srutzky - The truncation appears to be happening when the file is written. If I run the stored procedure that generates the data I get the
"Really long text...with ending"
correctly in the output window. But when we look in the file it's"Really long text... wit
, where thet
is the 256th character including the opening"
. Maybe it's an issue with the fact that I've had to quote the strings.– ChrisF
Feb 24 '17 at 15:34
I don't think it has to do with quoting the strings as I just put my SELECT into a global temp proc, added the quotes like you have, and it still saves correctly to the output file. Can you try wrapping the "long line" in a
CONVERT(VARCHAR(MAX), '"' + Column4 + '"') AS...
to see if that helps? Also, what version of SQLCMD are you using? I am using 11.0.2100.60.– Solomon Rutzky
Feb 24 '17 at 15:42
@srutzky - Nope. Still truncated. Version 10.50.4042.0
– ChrisF
Feb 24 '17 at 15:51
You are running SQL Server 2012 (I assume at least this given the use of
FORMAT
) yet the 2008 R2 version of SQLCMD? Any chance of finding (or even downloading) a newer version of SQLCMD?– Solomon Rutzky
Feb 24 '17 at 15:57