Padding Numbers Leading Zeros Using Transact-SQL

 
From
 
Use Str function which converts numeric data to a string.
 
SELECT replace(str(1,5),’ ‘,’0’)        — Outputs ‘00001’
SELECT replace(str(123,5),’ ‘,’0’) — Outputs ‘00123’
SELECT replace(str(12345,5),’ ‘,’0’)    — Outputs ‘12345’
SELECT replace(str(123456,5),’ ‘,’0’)   — Outputs ‘*****’
 
there is a third argument to str function which is no of decimals to keep
 
 
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: