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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

which function instead of rpad? 1

Status
Not open for further replies.

557

Programmer
Oct 25, 2004
64
US
if i have a number 0027 and i want it to have a length of 8 by adding 0s in front like 00000027, then what do i do? in oracle there is a function rpad. in sql server, is there any way to do this?
 
If it's a varchar field already (which would agree with how you worded the question), then just:
Code:
   [Blue]SELECT[/Blue] [Fuchsia]Right[/Fuchsia][Gray]([/Gray][red]'00000000'[/red][Gray]+[/Gray]YourField[Gray],[/Gray]8[Gray])[/Gray]
However, if the field is int, then assumming you don't have to worry about negatives:
Code:
   [Blue]SELECT[/Blue] [Fuchsia]Right[/Fuchsia][Gray]([/Gray][red]'00000000'[/red][Gray]+[/Gray][Fuchsia]Cast[/Fuchsia][Gray]([/Gray]YourField [Blue]AS[/Blue] [Blue]varchar[/Blue][Gray]([/Gray]8[Gray])[/Gray][Gray])[/Gray][Gray],[/Gray]8[Gray])[/Gray]
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
thanks a lot donutman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top