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: 3287
  • Last Modified:

Adding Leading Zeros

Is there a way to add leading zeros in a view in SQL Server?

There does not seem to be the equivalent to "format(myNum,'0000')" in SQL Server.
0
access_dude
Asked:
access_dude
1 Solution
 
helloknsCommented:
Try this,
SELECT REPLICATE( '0', 10 - LEN(@a))+@a

this will add a max of 10 leading zeroes.

here @a is varchar. If you have number, convert that to varchar using convert statement.
0
 
illCommented:
select Right('0000000000' + Left(123, 10), 10)
0
 
Scott PletcherSenior DBACommented:
What datatype is myNum?

If it's VARCHAR, you can do this:

SELECT RIGHT('0000' + myNum, 4)


If it's INT (or other numeric), you can do this:

SELECT RIGHT('0000' + CAST(myNum AS VARCHAR(4)), 4)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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