Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RIGHT ALIGN COLUMN DATA 1

Status
Not open for further replies.

emoongq

IS-IT--Management
Jun 3, 2004
23
MX
Hello I'm trying to right align a column to export into a text file. It is a numeric amount field and also need just 2 decimals.
 
You'll first need to convert it to a varchar and then padd it with spaces and finally, take just the characters you want.

Ex:

Code:
[COLOR=blue]Declare[/color] @Temp [COLOR=blue]Float[/color]

[COLOR=blue]Set[/color] @Temp = 0.83

[COLOR=green]-- Conver to decimal to round to 2 digits
[/color][COLOR=blue]Select[/color] [COLOR=#FF00FF]Convert[/color]([COLOR=blue]Decimal[/color](10,2), @Temp)

[COLOR=green]-- convert to varchar
[/color][COLOR=blue]Select[/color] [COLOR=#FF00FF]Convert[/color]([COLOR=blue]VarChar[/color](20), [COLOR=#FF00FF]Convert[/color]([COLOR=blue]Decimal[/color](10,2), @Temp))

[COLOR=green]-- Pad with spaces
[/color][COLOR=blue]Select[/color] [COLOR=#FF00FF]Replicate[/color]([COLOR=red]' '[/color], 20) + [COLOR=#FF00FF]Convert[/color]([COLOR=blue]VarChar[/color](20), [COLOR=#FF00FF]Convert[/color]([COLOR=blue]Decimal[/color](10,2), @Temp))

[COLOR=green]-- Finally, take just the right 20 characters
[/color][COLOR=blue]Select[/color] [COLOR=#FF00FF]Right[/color]([COLOR=#FF00FF]Replicate[/color]([COLOR=red]' '[/color], 20) + [COLOR=#FF00FF]Convert[/color]([COLOR=blue]VarChar[/color](20), [COLOR=#FF00FF]Convert[/color]([COLOR=blue]Decimal[/color](10,2), @Temp)), 20)

-George

"the screen with the little boxes in the window." - Moron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top