Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 607
  • Last Modified:

Format int to varchar in a specific way

Hey, I have an int field that has values between 0 and 9999.  I want to convert it to a varchar(4) so that values less than 1000 appear with leading zeroes.  For example, a value of 25 would appear 0025 or 789 would appears as 0789.  I want to do this in a select statement.  Thanks.
0
MariaHalt
Asked:
MariaHalt
  • 2
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Give this a whirl.  Test in SSMS by changing the @int value to anything you want, then once you're satisfied modify your code.

Declare @int int = 123

SELECT RIGHT('0000' + Cast(@int as varchar(10)),4)
0
 
MariaHaltAuthor Commented:
Perfect!  Thank you.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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