tags:

views:

66

answers:

1

Group, I need to remove the '-' from any negative numbers in a column for certain record numbers. However the Sum needs to * by .01 to get the correct format. I tried using replace but it is getting thrown off by the *.01 Below is my syntax.

CASE WHEN SUM(ExtPrice) *.01 < 0 AND RecordNum BETWEEN 4000 AND 5999 
        THEN REPLACE(SUM(ExtPrice) *.01,'-','')
     ELSE SUM(ExtPrice) *.01 
END AS Totals

For example SUM(ExtPrice) *.01 in one column gives me -5051.32 but when I use the above case statement I get 5050 another example -312.67 and I get 310 using the case. Any suggestions or better ways to do this are greatly appreciated.

+8  A: 

You can use the ABS function to get the positive value of a number. For example:

ABS(-123.445) /* this equals 123.445 */

So you can replace your CASE statement with:

CASE WHEN SUM(ExtPrice) < 0 AND RecordNum BETWEEN 4000 AND 5999         
         THEN ABS(SUM(ExtPrice) *.01)     
     ELSE SUM(ExtPrice) *.01 
END AS Totals
ichiban
Just CASE WHEN SUM(ExtPrice) < 0 to start seems better -- why multiply by .01 there, that doesn't change the sign.
Alex Martelli
@Alex - Good observation, I had copied the code down as it was.
ichiban