Search code examples
sqlformattingnumber-formatting

Display 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 that I would like the results to be displayed in units of thousands and inverted (negative values as positive and vice versa) with only one decimal place.

How can I get the positive values have a plus sign (+) in front of them just like the negative values have a dash sign (-) ?


Solution

  • 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