如何将各种中心转换为所需的5位数格式

时间:2015-03-05 21:58:59

标签: sql-server

我有一个colum LocationID是varchar(10),我需要将它们转换为NewLocation

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

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

我的concat语句无法评估何时在location_ID前面放置一个零或两个零。目标是使5位数的location_ID。

2 个答案:

答案 0 :(得分:0)

SELECT RIGHT('00000' + CAST(Location_ID  AS VARCHAR(5)) , 5)

答案 1 :(得分: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