Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 BCP to flat file with fixed length fields

Author  Topic 

karuna
Aged Yak Warrior

582 Posts

Posted - 2005-11-28 : 08:17:02
I have to export some of the data from my database to a flat file of fixed length fields. I'm thinking about bcp..any other suggesstions?

Since the output has to be fixed length lets say
column fname should be 10 character and age should be 3 char
if the column has the data something like this
fname age
kk 28
then in flat file kk should have 8 trailing spaces and 28 should be 028, how do I achieve this? After going through some threads in the forum,I tried with ansi_padding setting for fname column it didnt work. Here is the code

set ansi_defaults off
set ansi_padding on
create table test (fname varchar(10),age int)
insert into test
select 'kk',28
select * from test
drop table test
set ansi_defaults on
set ansi_padding off


Thanks

Karunakaran

X002548
Not Just a Number

15586 Posts

Posted - 2005-11-28 : 09:54:34
look up format cards in Books Online...and post the DDL of your table

What version of SQL Server?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

karuna
Aged Yak Warrior

582 Posts

Posted - 2005-11-28 : 09:58:37
quote:
Originally posted by X002548

look up format cards in Books Online...and post the DDL of your table

What version of SQL Server?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



EDIT: Oops...Didnt post the reply at all....

Here is the reply:
Version Sql2k EE. I looked up format file, couldnt understand it clearly. Bascially I want to push some data from 3-4 tables using joins, so I thought if I can get the output using some sample table I can apply the same for the original query.

Thanks
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-11-28 : 12:03:22
It would help if you read the hint link in my sig...

if you bcp out using dos, it will create a format file for you...

I use bcp a lot and it is easy (ok, it's easier the more you use it), but why not DTS?

besides DTS is going away, or it is being reincarnated...they didn't even mention it during the launch....

do a search here at sql team on format file..should be lots of examples....

but read the hint link



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2005-11-28 : 13:00:12
I usually do that by formatting the data in the query

select right(' ' + convert(varchar(3),age), 3)
+ left(convert(varchar(3),name) + space(80), 80)
+ ...

then use queryout in the bcp.
For format files have a look at
http://www.mindsdoor.net/SQLTsql/BCP_quoted_CSV_Format_file.html



==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

karuna
Aged Yak Warrior

582 Posts

Posted - 2005-11-29 : 08:15:22
quote:
Originally posted by X002548

It would help if you read the hint link in my sig...

if you bcp out using dos, it will create a format file for you...

I use bcp a lot and it is easy (ok, it's easier the more you use it), but why not DTS?

besides DTS is going away, or it is being reincarnated...they didn't even mention it during the launch....

do a search here at sql team on format file..should be lots of examples....

but read the hint link



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam




Brett,

I do agree the need to post the DDL and DML, not only to get the answers quick but also to save others time who help.There are some situations where you dont have the option of posting the same. I'm in a kind of that situation. Thats the main reason why I'm trying with some sample table I posted first.

My first option was DTS, but since I have not used BCP much, I thought I'll try with BCP and my belief is this could be easily done in BCP than going for DTS.

Thanks

Karunakaran
Go to Top of Page

karuna
Aged Yak Warrior

582 Posts

Posted - 2005-11-29 : 08:20:09
quote:
Originally posted by nr

I usually do that by formatting the data in the query

select right(' ' + convert(varchar(3),age), 3)
+ left(convert(varchar(3),name) + space(80), 80)
+ ...

then use queryout in the bcp.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.



I think this might be what I wanted..
Thanks nr

Karunakaran
Go to Top of Page
   

- Advertisement -