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

PADDING 0

Status
Not open for further replies.

lyudmila

Programmer
Oct 18, 2002
54
US
I have field in a table which has numeric(6,0)data type and has value range from 26 to 999999. I need to add leading zero's and have as finish result data type numeric(6,0). My values should be from 000026 to 999999
Example: 26 to 000026
154 to 000154
1008 to 001008
21009 to 021009
On old system it's done this way:

=zoned(substr(digits(itemnbr),1,5),6,0)
How I can do this in SQL Server?


 

SELECT REPLICATE('0',6-len(cast(pkID as varchar(6)))) & cast(pkID as varchar(6)) FROM myTable ---------------------------------------
[turkey] HAPPY THANKSGIVING!!!! [turkey]
mikewolf@tst-us.com
 
Hi Mike,
if I use your code my data type will be a CHAR(6),but I need NUMERIC(6,0). By the way to cancatenate these two expressions you need to use '+' not '&'.
 
print cast('000026' as numeric(6,0)) = 26
I thought you needed the result to be '000026'. A numeric datatype will not have leading zeros. You need a text string to have leading zeros, which is what my code gives you...


Sorry about the concatenation operation '&' vs '+' - I goofed on that one.
-----------------------------------------------------------------
[pc] Be nice. It's only doing what you tell it to do.
mikewolf@tst-us.com
 
Mike,
Thank you very much for your quick respond.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top