Show a positive result with a plus sign (+) in SQL

I have the following query:

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

The reason I have "/ 1000 * -1" is because I would like the results to be displayed in thousands and inverted (negative values ​​as positive and vice versa) with one decimal place.

How can I get positive values, they have a plus sign (+) in front of them, just as negative values ​​have a dash sign (-)?

+5
source share
2 answers
SELECT 
  case 
     when CONVERT(DECIMAL(11,1),SUM(Column/1000*-1)) >= 0 
     then concat('+', CONVERT(DECIMAL(11,1),SUM(Column/1000*-1))) 
     else CONVERT(DECIMAL(11,1),SUM(Column/1000*-1)) 
  end AS NAME
FROM Table
+5
source
SELECT 
  REPLACE(CONCAT('+', CONVERT(DECIMAL(11,1),SUM(Column/1000*-1))), '+-', '-')
FROM
  Table

This approach will show +0.0.

0
source

All Articles