BCP Imported File in UTF-8 Encoded File - sql

I have generated a CSV file using BULK Copy (BCP) utility in SQL Server 2008. I want a file that will be in UTF-8 Encoding. I am using below code to generate File. Any one can help me to resolve this problem.
DECLARE #Path VARCHAR(100) ,
#sql VARCHAR(8000) ,
#DBName VARCHAR(100) ,
#exePath VARCHAR(100),
SELECT #sql = 'bcp "SELECT * FROM '+#DBName+'..ABC" queryout '+#Path+'\'+'ABC'.csv -c -k -t, -U sa -P abc -T -S ' + ##servername
exec #exePath #sql;

Sql Server 2008 don't support utf-8 but you can use "-w" instead "-c" that will use UTF-16 as mentionned by DiMach, the probleme is that the csv file not will be well formed.
If you trying to export csv file for specific language, for example french (french contains spécial caracters : à é â ç ...) you will need to know that there is an other solution :
First : try to search in the internet the encoding that support all charaters of the target language, for example for french we can found ISO-8859-1
Second : search in the internet a page code related to encoding that you find, for ISO-8859-1 is 1252
Third : execute to following sql request in your sql server to see if your sql server support this encoding
select name, COLLATIONPROPERTY(name, 'CodePage') as Code_Page, description
from sys.fn_HelpCollations() where COLLATIONPROPERTY(name, 'CodePage')=1252 order by Code_Page
fourth : in BCP command use -C 1252 only (delete -c and -w)
I hope that's help

Just replace "-c" parameter with "-w"
SELECT #sql = 'bcp "SELECT * FROM '+#DBName+'..ABC" queryout '+#Path+'\'+'ABC'.csv -w -k -t, -U sa -P abc -T -S ' + ##servername

Related

SQL Server bcp utility export/write incomplete data

I am using SQL Server bcp utility to export data in csv format from a table.
I've noticed that sometimes the exported data is incomplete, I mean the bcp process exit without errors but the resulting csv file shows incomplete data at the end.
For example this is the bcp command and log of a recent export:
bcp "SELECT IdSap, CodSap, replace(replace(convert(nvarchar(max), Dati_XML), char(13), ''), char(10), '') as Dati_XML FROM MYDB.dbo.MyTable WHERE DataAggiornamento > '2019-06-07'" queryout "C:\temp\bcp_output.csv" -c -t 0x1f -U "myuser" -P "mypassword" -S "10.180.188.53"
As you can see there are no errors. The output file shows 100885 rows. The problem here is that the query should return 292887 rows, not 100884. It's like the bcp process has not finished to properly import/write data before exit.
Does anyone know why is this happening and any possible fix?

Export query result to csv in UTF-8

I would like to export query result to csv file in utf-8. Now i export to csv in this way:
DECLARE #cmd varchar(1000)
SET #cmd = 'bcp "select * from table" queryout "d:\textfile.csv" -w -T -t; -Slocalhost'
EXEC xp_cmdshell #cmd
How to make file be in UTF-8?
SQL Server does not support code page 65001 (UTF-8 encoding). reference
You need to add -w parameter in bcp utility to specify the encoding is UTF16.
I am sure you have long since solved your problem but as this is still open thought it might help someone else.
I am creating txt files with SQL Server queries and they seem to be "plain ANSI". In the end I used iconv.exe to convert from WINDOWS-1252 to UTF-8
iconv.exe is part of the GnuWin package which can be downloaded from Sourceforge https://sourceforge.net/projects/gnuwin32/

SQL Server BCP Empty File

I'm trying to use bcp to query out a comma-separated-value file but each time I get an empty file.
Here's my bcp command:
bcp "SELECT * FROM ##OutAK " QUERYOUT D:\Outbound\raw\li14090413.raw -c -T -t -S DB1
I have verified that ##OutAK is NOT empty because select count (*) from ##OutAK is not 0. When open file using HEX editor, I see the following:
0D 0A
I found the problem. It seems BCP is "allergic" with NULL. So, I just put ISNULL() to all the null-able fields and the output file is back to normal now.

My bcp hangs after creating an empty file

I am trying to drop a file into a directory on the local machine (same machine running SQL Instance). The content of the table I am trying to drop out is in xml format.
ie. table=xmlOutFiles, fieldName = xmlContent; fieldName contains essentially varchar(max) data that is to become the xml file we need.
When the bcp command is executed it seems to create the file in the #dest location, size = 0 bytes and then the process running from within SMSS just sits there waiting for something!
I cannot do anything with that empty file, like delete it, unless I use task manager to kill the process "bcp.exe".
I have tried multiple combinations of the bcp flags, etc.
Running the bcp command from a system prompt, replacing the "#vars" seems to work but I really need it to be part of my SQL Trigger script and function.
Assistance appreciated!!!!
Select #dest = (Select filename from xmlOutfiles)
Select #cmd = 'bcp "Select xmlContent from ProcureToPay.dbo.XmlOutFiles" queryout '+#dest+' -x -w -T -S' + ##servername
Exec xp_cmdshell #cmd
I have tried executing with -T and -Uusername -Ppassword parameters, etc.
This command works from the DOS prompt:
bcp "Select xmlContent from Procure.To.Pay.dbo.XmlOutFiles" queryout c:\temp\test.xml -x -w -T S<myservernameHere>

exporting query result to excel

I am trying to execute the below sql but I am getting "Invalid object name '.Sheet1$'."
INSERT INTO OPENDATASOURCE
('Microsoft.Jet.OLEDB.4.0',
'Database=c:\test.xls;Extended Properties=Excel 8.0')..[Sheet1$])
SELECT col1 FROM table;
its in mssql 2005.
any help is appreciated.
If you have xp_cmdshell enabled you can do this to export to delimited text file, which will open perfect in Excel.
EXEC xp_cmdshell 'SQLCMD -S [SERVERNAME] -d [DBNAME] -o "C:\Output.txt" -s "," -U "[USERNAME]" -P "[PASWORD]" -Q "SELECT TOP 10 * FROM table"';
According to this posting (and a few other samples Google found for me), you need three dots before the table: 8.0)...[Sheet1$]. (Don't ask me why).
Added: The German translation of this provides a complete example of Excel access:
SELECT * FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=C:\DataFolder\Documents\TestExcel.xls;Extended Properties=EXCEL 5.0')...[Sheet1$] ;