Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Statement

Status
Not open for further replies.

asafblasberg

Programmer
Jun 22, 2004
21
US
How would you implement this in an SQL Statement:

"The logic you could add to the Sql statement is to get the value from a different column in your table if the value you are fetching from the current column is NULL.

EXAMPLE:

Table1
Columns are: "A" and "B"
If the data in "A" is null, use the data in "B".

DATA:
A \ B
1 \ 2
***NULL \ 3

Result would output:
2, then 3 (since NULL is present in column "A"***

Thank you
Asaf
 
correction to my post:

"result output:
2, then 3"
no, it should be 1 then 3.

 
try something like

Code:
create table aa_test
(field1 int, field2 int)
go
insert into aa_test values(1, 11)
go
insert into aa_test values(NULL, 13)
go

select
	case when field1 is null then field2
	else field1
	end
from aa_test


"I'm living so far beyond my income that we may almost be said to be living apart
 
Now I'd like to take it a step further, i need to display other fields, so how would I change this...

select [field3], [field4] from mytable
case when field1 is null then field2
else field1
end

I get error message.
 
Code:
select [field3], [field4], 
case when field1 is null then field2
    else field1
    end AS FieldName
from mytable

Questions about posting. See faq183-874
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top