There is no built-in function that provides LPAD kind of functionality in MSSQL. Oracle has such built-in functionality available.
Using following code we can add leading Zeros with any number for specific length of string.
Example 1:
- DECLARE @data int = 10
- DECLARE @len int = 5
- Select right(replicate('0',@len) + cast(@data as varchar(50)), @len)
Result:
00010
Example 2:
- DECLARE @data int = 4583
- DECLARE @len int = 5
- Select right(replicate('0',@len) + cast(@data as varchar(50)), @len)
Result:
04583
04583
