Hi,
Today i came across the topic : How to Export the SQL Result set into different output files like Excel/CSV/Text.
we have different methodologies to select the SQL result set into different destinations. But for
Now, i am showing how can we export the data from SQL Server to csv file using bcp (Bulk Copy Program).
Standard BCP format looks like:
bcp [database_name.] schema.{table_name | view_name | "query"
Now the work around the task:
Create one test table.
We can not directly run the BCP in command prompt to export result set from sql to file. if so, will not get proper required format data.
For this, first i have created on script to select the data as shown in below.
SET NOCOUNT ON -- Used to not allow the number of rows selected
SELECT
[ID] AS [ID],
[Float] ,
convert(varchar(19), [DateTime], 120) as [DateTime],
Quotename([nvarchar],'"') as [Nvarchar]
FROM ExcelTest
Also, create one path for destination file store on your local machine and place the sql script file over there.
Run the below script under command prompt.
C:\vpeda\Test_Files\test>sqlcmd -S "CDTSSQL385D\MSSQL2K8R2" -d "Test_DB" -E -s "
|" -W -i test.sql | findstr /V /C:"-" /B>ExcelTest10.csv
Parameters:
-S : Server with instance
-d : Database Name
-E : Trusted Connection
-s : Delimiter specification (Column Seperator)
-W : Space removal
-i : Input data script file (if you are not running in local drive then give full path of the file)
findstr /V /C: remove the special characters
-o or > : Output file (if you are not running in local drive then give full path of the file).
Reference: https://www.excel-sql-server.com/sql-server-export-to-excel-using-bcp-sqlcmd-csv.htm#SQL
Thanks!
Venky
Today i came across the topic : How to Export the SQL Result set into different output files like Excel/CSV/Text.
we have different methodologies to select the SQL result set into different destinations. But for
Now, i am showing how can we export the data from SQL Server to csv file using bcp (Bulk Copy Program).
Standard BCP format looks like:
bcp [database_name.] schema.{table_name | view_name | "query"
{in data_file | out data_file | queryout data_file | format nul}
[-a packet_size]
[-b batch_size]
[-c]
[-C { ACP | OEM | RAW | code_page } ]
[-d database_name]
[-e err_file]
[-E]
[-f format_file]
[-F first_row]
[-h"hint [,...n]"]
[-i input_file]
[-k]
[-K application_intent]
[-L last_row]
[-m max_errors]
[-n]
[-N]
[-o output_file]
[-P password]
[-q]
[-r row_term]
[-R]
[-S [server_name[\instance_name]]
[-t field_term]
[-T]
[-U login_id]
[-v]
[-V (80 | 90 | 100 | 110 | 120 | 130 ) ]
[-w]
[-x]
Now the work around the task:
Create one test table.
CREATE TABLE [dbo04].[ExcelTest]( [ID] [int] NOT NULL, [Float] [float] NULL, [Datetime] [datetime] NULL, [Nvarchar] [nvarchar](255) NULL )
Test Data:
INSERT [dbo04].[ExcelTest] ([ID], [Float], [Datetime], [Nvarchar]) VALUES (1, 123.4567, '20110617 01:00:00', N'Hello, SQL Server!') (1, NULL, NULL, N'Hello, Excel!')Expected Results in the CSV file:
Id,Float,Datetime,Nvarchar 1,123.4567,2011-06-17 01:00:00,"Hello, SQL Server!" 2,,,"Hello, Excel!"
We can not directly run the BCP in command prompt to export result set from sql to file. if so, will not get proper required format data.
For this, first i have created on script to select the data as shown in below.
SET NOCOUNT ON -- Used to not allow the number of rows selected
SELECT
[ID] AS [ID],
[Float] ,
convert(varchar(19), [DateTime], 120) as [DateTime],
Quotename([nvarchar],'"') as [Nvarchar]
FROM ExcelTest
Also, create one path for destination file store on your local machine and place the sql script file over there.
Run the below script under command prompt.
C:\vpeda\Test_Files\test>sqlcmd -S "CDTSSQL385D\MSSQL2K8R2" -d "Test_DB" -E -s "
|" -W -i test.sql | findstr /V /C:"-" /B>ExcelTest10.csv
Parameters:
-S : Server with instance
-d : Database Name
-E : Trusted Connection
-s : Delimiter specification (Column Seperator)
-W : Space removal
-i : Input data script file (if you are not running in local drive then give full path of the file)
findstr /V /C: remove the special characters
-o or > : Output file (if you are not running in local drive then give full path of the file).
Reference: https://www.excel-sql-server.com/sql-server-export-to-excel-using-bcp-sqlcmd-csv.htm#SQL
Thanks!
Venky