Pad a string with leading zeros so it's 3 characters long in SQL Server 2008

后端 未结 17 1290
清歌不尽
清歌不尽 2020-11-22 08:13

I have a string that is up to 3 characters long when it\'s first created in SQL Server 2008 R2.

I would like to pad it with leading zeros, so if its original value w

17条回答
  •  忘掉有多难
    2020-11-22 08:43

    I had similar problem with integer column as input when I needed fixed sized varchar (or string) output. For instance, 1 to '01', 12 to '12'. This code works:

    SELECT RIGHT(CONCAT('00',field::text),2)
    

    If the input is also a column of varchar, you can avoid the casting part.

提交回复
热议问题