转换为float并在SQL Server上向左移动零

Gol*_*old 2 sql-server transactions

我需要转换为float并在SQL Server中将零填充为零.

我需要这种格式####.##.

例如:

2.23   ->  0002.23
123.3  ->  0123.30
12.65  ->  0012.65
34     ->  0034.00
Run Code Online (Sandbox Code Playgroud)

我试过这个:

cast(Weight as decimal(6, 2))
Run Code Online (Sandbox Code Playgroud)

还有这个:

REPLACE(STR(Weight, 6), SPACE(1), '0')
Run Code Online (Sandbox Code Playgroud)

还有这个:

REPLACE(STR(cast(Weight as decimal(6,2)), 6), SPACE(1), '0')
Run Code Online (Sandbox Code Playgroud)

但它们不起作用 - 我得到5.00或者000005我需要0005.00.

谢谢

Joh*_*tti 5

如果是SQL Server 2012+,则FORMAT()是一个选项.

Select format(123.3,'0000.00')
Run Code Online (Sandbox Code Playgroud)

返回

0123.30
Run Code Online (Sandbox Code Playgroud)

Format() 有一些很好的功能,但不知道它的性能