简体   繁体   English

SQL Server:为什么FORMAT在参数为零时返回空字符串?

[英]SQL Server: Why does FORMAT return an empty string when the parameter is a zero?

I'm dealing with a weird bug, when converting numbers to text. 在将数字转换为文本时,我正在处理一个奇怪的错误。

Whenever the number is zero, FORMAT returns an empty string, instead of '0' 每当数字为零时, FORMAT返回一个空字符串,而不是'0'

Is there a reason for this behavior? 这种行为有原因吗?
Am I using the FORMAT function incorrectly? 我是否错误地使用了FORMAT功能?

SELECT FORMAT ( 1 , N'#.######################' )  /* result '1' */

SELECT FORMAT ( 1E0 , N'#.######################' )  /* result '1' */

SELECT FORMAT ( 1.0 , N'#.######################' )  /* result '1' */

SELECT FORMAT ( 0 , N'#.######################' )  /* result '' */

SELECT FORMAT ( 0E0 , N'#.######################' )  /* result '' */

SELECT FORMAT ( 0.0 , N'#.######################' )  /* result '' */

#.##### produces exactly the style we need, so we'd like to keep that if possible. #.#####产生了我们需要的风格,所以如果可能的话,我们想保留它。

As a workaround, I'm manually checking for zeros - but this is annoying, and kinda unscalable. 作为一种解决方法,我手动检查零 - 但这很烦人,有点不可扩展。

The # doesn't fill in zeroes while 0 does. 0为0时, #不填充零。 I think what you want is: 我想你想要的是:

SELECT FORMAT ( 0 , N'0.######################' ) 

See https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings 请参阅https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM