1

I need to pad my column left with zeros up to 7. Some are already 7 digits already but anything that isn't 7 digits needs a leading 0. What is the best way to code this?

Kevin M
  • 367
  • 2
  • 4
  • 7
  • I realize that the other link is for 3 digits instead of 7, but I think that you can probably extrapolate out there. Took all of 3 seconds to find that. – Tom H Mar 15 '16 at 16:22

1 Answers1

5

You can just do:

SELECT RIGHT('0000000'+CONVERT(VARCHAR(7),YourNumber),7)
FROM dbo.YourTable;
Lamak
  • 69,480
  • 12
  • 108
  • 116
  • Why doesn't this work? SELECT FORMAT(YourNumber, '0000000') FROM table – Kevin M Mar 15 '16 at 16:54
  • @KevinM Hadn't noticed that you were using SQL Server 2014, but well, that should work, what do you mean it doesn't? – Lamak Mar 15 '16 at 17:05