gpt4 book ai didi

sql - 在 SQL 中显示带有加号 (+) 的肯定结果

转载 作者:bug小助手 更新时间:2023-10-28 10:52:02 27 4
gpt4 key购买 nike

我有以下疑问:

    SELECT 
CONVERT(DECIMAL(11,1),SUM(Column/1000*-1)) AS NAME,
FROM
Table

我有“/1000*-1”的原因是我希望结果以千为单位显示并反转(负值表示正数,反之亦然),只有一位小数。

我怎样才能让正值前面有一个加号 (+),就像负值有一个破折号 (-) 一样?

最佳答案

您可以在 FORMAT 中使用 分号分隔 多部分字符串。 函数(有点像 Microsoft Excel 中的自定义数字格式)。

A number format can have up to three sections of formatting code, separated by semicolons. These code sections define the format for positive numbers, negative numbers, and zero values, in that order:

<POSITIVE>;<NEGATIVE>;<ZERO>

example:

FORMAT(@YourValue,'+0.0;-0.0')

(Adapted from this)


我通常在显示 +/- 时也隐藏零符号,所以我使用格式化字符串: '+0;-0;'''

SELECT FORMAT(+5,'+0;-0;''') --returns: +5
SELECT FORMAT(-5,'+0;-0;''') --returns: -5
SELECT FORMAT(-5,'+0;-0;''') --returns: <empty string>


要显示零,您可以使用格式化字符串: '+0;-0;0'


适用于: , , , ,


更多信息:

关于sql - 在 SQL 中显示带有加号 (+) 的肯定结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10795990/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com