Cristina
Cristina

Reputation: 21

How do I move the negative debit values from a column into the credit column and make them positive?

I have this table in SQL called 'account' that looks like this:

Debits  Credits

-22      0

-1      -1

-5       0

 0      -9

-13     -4

I want all the values to be positive, but for the account to still make sense at the end. So all the negative values in the Debits column will become positive in the Credits column so that it looks like the table below. How do I go about this? The values in both columns are floats.

Debits  Credits

0       22

1       1

0       5

9       0

4       13

Thanks!

Upvotes: 1

Views: 222

Answers (2)

David דודו Markovitz
David דודו Markovitz

Reputation: 44991

Use the unary minus operator for a cleaner syntax

UPDATE yourTable
SET
    Credits = -Debits,
    Debits = -Credits;

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522797

Your sample data implies that the logic is to swap and negate the debits and credits values:

UPDATE yourTable
SET
    Credits = -1.0 * Debits,
    Debits = -1.0 * Credits;

Demo

Upvotes: 1

Related Questions