BCP detects CRLF as data and not as Row ending - sql

I am using bcp to export and import data, for example:
bcp "exec db.dbo.procedure" queryout "C:\Users\I\Desktop\ps.dat" -c -r "*" -t; -S -U -P
bcp db.dbo.table in C:\Users\I\Desktop\ps.dat -e "C:\Users\I\Desktop\ps_error.dat" -c -r "*" -t; -m1000000 -S -U -P
If I execute these statements without -r, bcp uses the default CRLF as end of row. Later, the import fails with right data truncation.
After so many attempts I have seen that CRLF is detected as two bytes of data, and it does not fit the table format. When I use the above statements it works perfectly.
Why is this happening? Is this a bcp bug, or is the expected behaviour?

According to MS that is the expected behaviour:
https://learn.microsoft.com/en-us/sql/tools/bcp-utility
this article explains all the parameters and for this case these are the ones we are interested in:
-c
Performs the operation using a character data type. This option does not prompt for each field; it uses char as the storage type, without prefixes and with \t (tab character) as the field separator and \r\n (newline character) as the row terminator. -c is not compatible with -w.
-r
row_term
Specifies the row terminator. The default is \n (newline character). Use this parameter to override the default row terminator. For more information, see Specify Field and Row Terminators (SQL Server).
So it seems that by removing -r which sets the row terminator to \n (LF) , -c is taking over and setting the row terminator to \r\n (CRLF)

Related

impala shell output csv file generation with enclosure

I am new to Impala. I am trying to fetch data from a table and load it to csv file. But I want to enclose the data in double quotes as there is a conflict in delimiter. How can I enclose the data with double quotes for each field?
query = "select t1max,t2max,rest_call from topo_tax"
result_string = 'env -i /usr/bin/impala-shell -i "'+ impalad+':'+port +'" -u "'+user+'" -d "'+database+'" -B --delimited -q "'+query+'"' ' -o /tmp/data_dump.csv --print_header --output_delimiter=,'
As a workaround, you might give delimiter as ","

Save column value to file SQL Server

How can I save first column from first row from query to file without additional character?
When I save data like this:
EXEC xp_cmdshell 'BCP "SELECT ''xxx'' " queryout D:\file.txt -w -T -S OMD-MG\SQL2008R2'
I've got:
additional \r\n at the end of file
When I save data like that:
EXEC xp_cmdshell 'BCP "SELECT ''xxx'' " queryout D:\file.txt -N -T -S OMD-MG\SQL2008R2'
I've got:
additional characters at front of file I think this is length
I try many parameter without satisfied result
Is there other option to save data to file from query without designer or management studio with correct data?
-N is a native binary format where any nullable or variable length fields are preceeded by their length. If you use -N with a non nullable, fixed-width field it will not be preceeded by its length.
If you want text data without the newlines you could try -r '' to specify the row terminator which is \n by default, e.g.:
bcp "select 'xxx'" queryout test.txt -c -t '' -r ''
..at least in SQL Server 2016 CTP I'm seeing that BCP tries to add padding to varchar columns. If you convert to text it seems to work alright:
bcp "select convert(text, col) from table" queryout file -c -t '' -r ''

BCP command insert text file to SQL table

I am trying to import into SQL a text file using this BCP command:
bcp test.dbo.bcp2 in C:\Test\test.txt -c -t -SSQServer -U user -P
test1 -t \t -r\n -e C:\Test\error.txt
The text.txt file has \t as column delimiter and \n as row delimiter.
The error received is Unexpected EOF.
I can confirm that the SQL table has the right table definition so there should not be any conversion errors.
i think no need to put any delimiter have text which is well arranged example if you have data in the excel copy and paste it in the text file and run the command
BCP tablename in c:\test.txt -S server name -Uuserid -Ppassword -c

Specify column separator for BCP

How do I specify column separator with "," for bcp or this code below
bcp [wwtest].[accesscontrol].[Roles] out C:\test\bcp_outputTable.csv -SPC01 -T -c
Try adding -t, . See following documentation : https://msdn.microsoft.com/en-us/library/ms191485.aspx and https://msdn.microsoft.com/en-us/library/ms162802.aspx
The command lines below illustrate how to export data using a comma (,) or vertical pipe (|) as a field separator:
Export using comma:
BCP [Database].[Schema].[Table] out C:\FOLDERNAME\FILENAME.DAT -c -t, -S SERVERNAME -T
Export using vertical pipe:
BCP [Database].[Schema].[Table] out C:\FOLDERNAME\FILENAME.DAT -c -t^| -S SERVERNAME -T
The vertical pipe is different because it must be escaped. Quoting -t| will not work.
I hope this helps.
Please note command line switches are case sensitive.

Specify rowterminator when creating a file with SQLCMD

How do you specify the the row terminator when outputting query results using sqlcmd?
bcp is not an option.
sqlcmd -E -s" " -Q "Select * From SomeTable" -o C:\Output.txt -W
What is the default row terminator?
You can only choose column separator with -s. There is not option to specify row terminator.
So it is CR + LF because the output goes to command line. However, data may be truncated so you have to control the output width.
See sqlcmd for -s, -w, -W, -y, -Y etc
bcp may not be an option, as you say, but neither is row terminator in sqlcmd...