Drew L. Facchiano
Drew L. Facchiano

Reputation: 291

Add a new column with the diff of two values

SQL is goofy sometimes ain't it? Right now I want to take two values from different column and compare the difference. Ex:

ColA | ColB | New column
1    |  0   |  1
2    |  5   |  3
3    | 10   |  7

What should I do i order to create this RAD new column?

Upvotes: 1

Views: 157

Answers (3)

Derrick Moeller
Derrick Moeller

Reputation: 4960

Do you actually need a new column? Perhaps you just want access to this value in a select statement?

SELECT ColA, ColB, ABS(ColA - ColB) AS [New column]
FROM YourTable

Upvotes: 0

Lukasz Szozda
Lukasz Szozda

Reputation: 176124

You could use computed/calculated column:

ALTER TABLE tab_name
ADD new_column AS (ABS(ColB - ColA));

DBFiddle Demo

Upvotes: 2

Travis
Travis

Reputation: 687

Maybe something like this...

select ColA, ColB, ABS(ColA-ColB) as 'New column' from table 

Upvotes: 0

Related Questions