Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I need to get my query result in "csv" file format with '|' Symbol as my delimiter.
for example I am having a query like
SQL
SELECT Ord_ID, Ord_Amt FROM OrderDtls 

The above Query result, I need to store in 'CSV' file with '|' as my delimiter.

Regards,
GVPrabu
Posted
Updated 20-May-13 3:42am
v3

See http://www.connectionstrings.com/textfile[^] for details of how to change the default separator.
 
Share this answer
 
Comments
gvprabu 20-May-13 11:10am    
Hi,
I am not getting any answers related to my question in that link....
I need to do this in SQL Server
Richard MacCutchan 20-May-13 11:21am    
You may need to use the management interface; it's not something I have tried. Can you read a normal comma separated .CSV file through SQL server?
gvprabu 20-May-13 11:27am    
yes correct, I want to know the Management Interface setting Details for getting this, yes in SQL Serve Management studio is giving separator as "comma or space". I need to change with '|'. ok fine thank you.
Richard MacCutchan 20-May-13 11:45am    
As I said, this is not something I have used. You will need to, do some research/read the documentation, in order to find out how to do it.
gvprabu 20-May-13 12:00pm    
sure, thanks for ur reply...
I found the solution....

We can use the following SQL Command for this...
SQL
EXECUTE master.dbo.xp_cmdshell 'bcp "SELECT object_name(object_id) As TbLName,name as ColName FROM SandBox.sys.columns  ORDER BY object_name(object_id), column_id" queryout C:\Data\Objects.txt -t"|" -c -T ' 


or else we will do the following Management Studio Settings...

If your using Microsoft Sql Server management studio this is Configurable.
(actually almost the exact same in query analyzer)

Tool--> Options -->Query Results --> Sql Server --> Results to Text.

Change output format from Tab Delimited to Custom Delimited.
Put a Pipe in the Custom Delimiter Box.

Go back to your query select Query --> Results to File. Execute file, Give it a Disk path and a name.

The file will be your name with a .rpt Extension, you can rename it to have a .txt extension.

You obviously have to set the results to text value back to your previous setting if you don't want this permanently.

Works good for a one time once in a while thing. but if you must generate this file frequently I would look at creating a SSIS Package. or as suggested above.

Regards,
GVPrabu
 
Share this answer
 
just copy and paste this code in your sql editor
you will find a *.csv file in you C: drive
SQL
DECLARE @Ord_ID varchar(50)
DECLARE  @Return varchar(50)
DECLARE @Ord_Amt INT
DECLARE @cmd Nvarchar(2000)
DECLARE @Order CURSOR
SET @Order = CURSOR FOR
SELECT Ord_ID, Ord_Amt FROM OrderDtls
OPEN @Order
FETCH NEXT
FROM @Order INTO @Ord_ID,@Ord_Amt
WHILE @@FETCH_STATUS = 0
BEGIN
set @cmd = 'echo ' +  @Ord_ID+ ';' + convert(varchar(50), @Ord_Amt) + ' >>c:\myTable.csv'--set @cmd = 'echo ' + @Ord_ID+' >>c:\myTable.csv'
EXEC @Return = MASTER..xp_cmdshell @cmd 
FETCH NEXT
FROM  @Order INTO @Ord_ID,@Ord_Amt
END
CLOSE @Order
DEALLOCATE @Order
 
Share this answer
 
v4
Comments
gvprabu 21-May-13 5:43am    
hi, Which delimiter it will take. I need pipe(|) symbol as delimiter. So how we will change the default delimiter in your query.?
Basmeh Awad 21-May-13 9:52am    
it is accepting semicolan(;) but not accepting (|)Pipe
gvprabu 21-May-13 10:30am    
ok fine, I got the solution(Solution 2)... thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900