Skip to main content

SQL PadLeft

· One min read
Alec.Ji
  • SQL
PRINT RIGHT(CAST(REPLICATE('0', LEN(no_start)) + CAST(no_start + (inventory - 1) AS VARCHAR(50)) AS VARCHAR(50)), LEN(no_start))

PRINT RIGHT(CAST(REPLICATE('0', LEN('000001')) + CAST('000001' + (50 - 1) AS VARCHAR(50)) AS VARCHAR(50)), LEN('000001'))
  • Result
000050

Test

Test Sql

PRINT RIGHT(CAST(REPLICATE('0', LEN('000001')) + CAST('000001' + (50 - 1) AS VARCHAR(50)) AS VARCHAR(50)), LEN('000001'))

PRINT REPLICATE('0',5)

PRINT REPLICATE('0',5) + 123

PRINT REPLICATE('0',5) + '123'

PRINT REPLICATE('0',5) + STR(123)

PRINT REPLICATE('0',5) + CAST(123 AS varchar(20))

PRINT STR(123)

PRINT STR(123,5,2)

PRINT STR(12345678900)

Test Result

000050
00000
123
00000123
00000 123
00000123
123
123.0

REF

C# PadLeft
SQL REPLICATE
SQL STR