x

Selecting specific fields to make a CSV file

I want to extract the data for selected months in csv format, but the thing is, i dont need all the fields to be in my output file, just want to extract for some selected fields.

For eg: I need fname, lname, mobile, emailid

I am using SQL 2005.

more ▼

asked Oct 29, 2009 at 10:10 AM in Default

sqlqueries000 gravatar image

sqlqueries000
11 1 1 1

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

If you are using some external tool to make the CSV file and you can not (why not?) specify the specific fields you need then you could make a VIEW and query that instead:

CREATE VIEW MyView
AS
SELECT MyCol1,
MyCol2,
...
FROM MyTable
more ▼

answered Oct 29, 2009 at 10:21 AM

Kristen gravatar image

Kristen ♦
2.2k 6 7 10

(comments are locked)
10|1200 characters needed characters left

or use bcp command and select the required columns


exec xp_cmdshell 'bcp "select col1,col2..colN from dbname..table"
queryout "C:\test.csv" -T'
more ▼

answered Nov 13, 2009 at 03:41 AM

Madhivanan gravatar image

Madhivanan
1.1k 1 3 6

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1951
x35

asked: Oct 29, 2009 at 10:10 AM

Seen: 1191 times

Last Updated: Oct 29, 2009 at 10:23 AM