• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 16520
  • Last Modified:

How do I left pad a string expression with zero's?

In an sp i'm writing, I need to convert an integer value to a string. I usually do this with the cast() function. But this time, I need to left pad the string with zeros. So how do I do this?

Ian
0
ipendlebury
Asked:
ipendlebury
3 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
unless you do that in the front-end code, here we go:

select right('000000000' + cast(yourfield as varchar(10)), 10)
0
 
derekkrommCommented:
Where @len = length of desired string:

select replicate('0', @len - len(cast(number as varchar))) + cast(number as varchar)
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
> I need to convert an integer value to a string

Eg:


 
SELECT RIGHT('000000'+CAST(1 AS VARCHAR),5)
0
 
ipendleburyAuthor Commented:
Three prompt replies. Thank you all for your help.

Ian
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now