0

I have a colum LocationID which is varchar(10) and I need to convert them to NewLocation

Location_ID     NewLocation
123             00123
2233            02233
42345           42345
22              00022
10              00010 

Select (Concat('00'+Location_ID)) as NewLocation
from tablelocation;

My concat statement can not evaluate when to place one zero or two zero in front of location_ID. The goal is to make 5 digit location_ID.

Marco
  • 22,856
  • 9
  • 75
  • 124
amanda
  • 1
  • 2
    possible duplicate of [Pad a string with leading zeros so its 3 characters long in SQL Server 2008](http://stackoverflow.com/questions/16760900/pad-a-string-with-leading-zeros-so-its-3-characters-long-in-sql-server-2008) – Marco Mar 05 '15 at 22:05

2 Answers2

0
SELECT RIGHT('00000' + CAST(Location_ID  AS VARCHAR(5)) , 5)
M.Ali
  • 67,945
  • 13
  • 101
  • 127
0
declare @t table (ID INT)
insert into @t(ID)values (123)
insert into @t(ID)values (42345)
insert into @t(ID)values (23)
insert into @t(ID)values (22)
insert into @t(ID)values (10)


DECLARE @Length int
SET @Length = 5


select right( POWER(10, @Length) + ID, @Length) from @t
mohan111
  • 8,633
  • 4
  • 28
  • 55