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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query Help

Status
Not open for further replies.

awholtsIT

IS-IT--Management
Aug 18, 2008
27
US
I want to query a column from a table, evaluate the value in that column (column 1). If the value in column 1 is > 0, input that value into column 2, if the value in column A is < 0, input that value from column 1 into column 3.



ex: column 1 column 2 column 3

-5 -5
5 5

The end result should look like the above illustration.

Appreciate help with the programming of the query/script to accomplish this.

Thanks,

Andrew
 
select Field1, case when Field1 > 0 then Field1 else null as Field2 end, case when Field <=0 then Field1 else null as Field3 end from myTable
 
I'm having a problem with the syntax.

I have written the following;

select b.field1,
case when b.field1 > 0 then b.field1
else null as b.field2
end
case when b.field1 =< 0 then b.field1
else null as b.field3
end
from MyTable

I get the following error message;

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'as'.

Can you see what I'm doing wrong?

Thanks,

Andrew
 
... Else Null end As Field2...

[pipe]
Daniel Vlas
Systems Consultant

 
Oh, my g-d! I'm sorry, wrote this from the top of my head and put END in the wrong spot!

Should have been

(CASE WHEN myField1>0 THEN myField1 ELSE NULL END) as Field2
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top