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)
 Fixed width columns

Author  Topic 

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2006-09-13 : 04:38:18
Hi,

I need to create a fixed width flat file and fill the columns with special characters if they are not wide enough. For text-type data I typically use something like this

SELECT LEFT(myColumn + SPACE(50), 50)

to create a fixed width column of 50 characters but sometimes I need to fill with differentcharacters than space. Any nice ways to do this?

--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-09-13 : 04:41:06
use replicate()


KH

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-09-13 : 04:41:24
REPLICATE instead of SPACE ?



Kristen
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2006-09-13 : 05:20:15
Ah...hehe...I actually looked at REPLICATE in BOL but the example replicated a columnname in the adventureworks database like this:

SELECT REPLICATE (LastName, 2) AS "LastName Twice" FROM Employees

Result set:
--------------
FunkFunk

I didn't see that I could use it for what I wanted...how stupid can you get!



--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-09-13 : 05:50:50
"I didn't see that I could use it for what I wanted"

No kidding - what use is a "LastName Twice" column anyways??

Any, more seriously, why have they switched from [ColumnName] back to the old "ColumnName" style?

Kristen
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-09-13 : 05:58:27
"I didn't see that I could use it for what I wanted"
Why can't you use REPLICATE?

Select LEFT(myColumn + Replicate('#',50), 50)


Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-09-13 : 06:25:09
Lumbago can, but it wasn't obvious to him from BoL - and I agree based on that example!

Kristen
Go to Top of Page
   

- Advertisement -