0

如何将花括号添加到负值,这将是两列相减的结果,

假设我有三列

        A      B           result(B-A)
-----------------------------------------
        1      4             3

        2      1            -1 [should be displayed as (1) not -1]

        9      3            (6)
-----------------------------------------
total   3      5            (4)

如何在选择查询中执行此操作?

4

2 回答 2

1

如果你使用 sql server 试试这个:

select case  
       when (b-a) < 0 then  '{ ' + substring(CONVERT(NVARCHAR,(b-a)),2,2 ) +  '}' 
       else   CONVERT(nvarchar(max),(b -a)) END as result
from table
于 2012-12-05T12:19:37.320 回答
0

询问:

SQLFiddle示例

SELECT
t1.A,
t1.B,
CASE WHEN t1.B-t1.A<0 
      THEN '('+CAST( (t1.B-t1.A)*-1 as varchar(5))+')'
      ELSE CAST( t1.B-t1.A as varchar(5))  END AS result
FROM tbl t1

结果:

| A | B | RESULT |
------------------
| 1 | 4 |      3 |
| 2 | 1 |    (1) |
| 9 | 3 |    (6) |
于 2012-12-05T12:35:39.930 回答