SQL PadLeft

SQL PadLeft

  • SQL
1
2
3
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
    1
    000050

Test

Test Sql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

1
2
3
4
5
6
7
8
000050
00000
123
00000123
00000 123
00000123
123
123.0

REF

C# PadLeft

SQL REPLICATE

SQL STR

作者

Alec.ji

发布于

2018-05-01

更新于

2021-11-01

许可协议

评论