snippetsqlMinor
How to extract pipe-delimited data using BCP?
Viewed 0 times
howdelimitedpipebcpusingextractdata
Problem
I tried the following command
I want to get the data in following format.
Format will be Pipe delimited text file, with double quote text qualifiers and no header row. How to modify or write the BCP command to get the required result.
My dataset is just a customer table.
Required data format is
USE
SELECT 'exec master..xp_cmdshell'
+ ' '''
+ 'bcp'
+ ' ' + TABLE_CATALOG + '.' + TABLE_SCHEMA + '.' + TABLE_NAME
+ ' out'
+ ' D:\'
+ TABLE_NAME + '.csv'
+ ' -c'
+ ' -t,'
+ ' -T'
+ ' -S' + @@SERVERNAME
+ ''''
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'I want to get the data in following format.
Format will be Pipe delimited text file, with double quote text qualifiers and no header row. How to modify or write the BCP command to get the required result.
My dataset is just a customer table.
CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 GermanyRequired data format is
1|"Alfreds Futterkiste"|"Maria Anders"|"Obere Str. 57 Berlin"|12209|"Germany"Solution
Now that I better understand that you are trying to extract data, here is a new answer. This just simply extracts data from a table that I created in tempdb.
Which results in the following bcp command
Update This bcp command will add double quotes
or just with bcp
This bcp command uses the quotename function. Which allows you to delimit your output using whatever characters you like.
use tempdb
-- drop table mytable
create table mytable
(id int, customer_name varchar(55), cityname varchar(55), statename varchar(55))
insert into mytable
values (1, 'a', 'a city', 'a state')
, (2, 'b', 'b city', 'b state')
, (3, 'c', 'c city', 'c state')
, (4, 'd', 'd city', 'd state')
select cast(id as varchar(10)), customer_name, cityname, statename from mytableWhich results in the following bcp command
bcp "select cast(id as varchar(10)), customer_name, cityname, statename from mytable" queryout c:\temp\myTable.csv /S.\instance_name /d tempdb /c /t"|" -TUpdate This bcp command will add double quotes
exec Master..xp_Cmdshell 'bcp "select cast(id as varchar(10)), quotename(customer_name,char(34)), quotename(cityname,char(34)), quotename(statename,char(34)) from mytable" queryout "c:\temp\myTable.csv" /S.\instance_name /d tempdb /c /t"|" /T'or just with bcp
bcp "select cast(id as varchar(10)), quotename(customer_name,char(34)), quotename(cityname,char(34)), quotename(statename,char(34)) from mytable" queryout "c:\temp\myTable.csv" /S.\instance_name /d tempdb /c /t"|" /TThis bcp command uses the quotename function. Which allows you to delimit your output using whatever characters you like.
Code Snippets
use tempdb
-- drop table mytable
create table mytable
(id int, customer_name varchar(55), cityname varchar(55), statename varchar(55))
insert into mytable
values (1, 'a', 'a city', 'a state')
, (2, 'b', 'b city', 'b state')
, (3, 'c', 'c city', 'c state')
, (4, 'd', 'd city', 'd state')
select cast(id as varchar(10)), customer_name, cityname, statename from mytablebcp "select cast(id as varchar(10)), customer_name, cityname, statename from mytable" queryout c:\temp\myTable.csv /S.\instance_name /d tempdb /c /t"|" -Texec Master..xp_Cmdshell 'bcp "select cast(id as varchar(10)), quotename(customer_name,char(34)), quotename(cityname,char(34)), quotename(statename,char(34)) from mytable" queryout "c:\temp\myTable.csv" /S.\instance_name /d tempdb /c /t"|" /T'bcp "select cast(id as varchar(10)), quotename(customer_name,char(34)), quotename(cityname,char(34)), quotename(statename,char(34)) from mytable" queryout "c:\temp\myTable.csv" /S.\instance_name /d tempdb /c /t"|" /TContext
StackExchange Database Administrators Q#111378, answer score: 8
Revisions (0)
No revisions yet.