how to convert an integer to string and left pad it with zero’s?


Here’s a quick and simple way of converting an integer to string (varchar) and left pad it with leading zeros like 00012. This example will get the result in 5 digits

select right(‘00000’ + cast(yourfield as varchar(5)), 5)

Advertisements

Author: Imran Akram

A .NET/SharePoint Consultant and a political enthusiast who believes in putting the feet of the elected and/or unelected officials to fire in order to get good governance.

6 thoughts on “how to convert an integer to string and left pad it with zero’s?”

  1. Nice!

    I`l use it in my tree sort stuff, like

    update #Tree
    set ObjectSortExpression = stuff(ObjectSortExpression, (ObjectLevel-1)*5 + 1, 5, cast(ObjectNumber as char(5)))
    where ObjectType = ‘Station’

    That require replace of cast(ObjectNumber as char(5) into your variant

  2. Pingback: Photoshop

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s