PadLeft function in T-SQL

后端 未结 17 2823
挽巷
挽巷 2020-11-27 03:44

I have the following table A:

id
----
1
2
12
123
1234

I need to left-pad the id values with zero\'s:

id
----
0         


        
17条回答
  •  自闭症患者
    2020-11-27 03:55

    This works for strings, integers and numeric:

    SELECT CONCAT(REPLICATE('0', 4 - LEN(id)), id)

    Where 4 is desired length. Works for numbers with more than 4 digits, returns empty string on NULL value.

提交回复
热议问题