Reputation: 854
How do I create a calculated column in my SELECT QUERY where it has a condition?
Something like:
SELECT NAME, ADDRESS, IF TABLE.COD=101 THEN return TABLE.VALUE AS TABLE
FROM CRM
The third argument in the SELECT is my calculated column with a condition.
Upvotes: 1
Views: 1086
Reputation: 370
SELECT NAME, ADDRESS, (Case WHEN TABLE.COD=101 THEN TABLE.VALUE END) AS TABLE
FROM CRM
Upvotes: 2
Reputation: 16045
Another option would be using the IIF
function.
Frankly, the IIF
is function only by name, by essence it is a shorthand, a macro substitution expanding into the CASE
statement.
That close relationship probably was why you mistaken IF
and CASE
when writing your statement.
SELECT NAME, ADDRESS, IIF(TABLE.COD=101, TABLE.VALUE, NULL) as TABLE FROM CRM
Now, your statement seems not being real. You refer to the TABLE.COD
and TABLE.VALUE
- those are clearly some fields from some second table, but you forgot to include it in your FROM
clause!
Because of that I think what you really may need is not some condition/calculation, but a plain old OUTER JOIN
.
https://en.wikipedia.org/wiki/Join_(SQL)
SELECT A.NAME, A,ADDRESS, B.VALUE as TABLE
FROM CRM A
LEFT JOIN SomeTable B ON B.COD=101 AND B.XXXX = A.YYYY
Upvotes: 1