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

Select Case statement in SQL server

Status
Not open for further replies.

namas

Technical User
Aug 31, 2006
31
US
In SQL server 2000, following error shows up when below SQL is run?
How to fix it? I want to display a Debit column if gl_amount > 0 and
Credit Column if gl_amount < 0. Thanks.

Server: Msg 170, Level 15, State 1, Line 4
Line 4: Incorrect syntax near '>'.

SELECT gl_detail.gl_year, gl_amount,

Debit = case gl_amount
when gl_amount > 0 then gl_amount
else 0
end,
credit = case gl_amount
when gl_amount < 0 then gl_amount * -1
else 0
end
FROM gl_detail
 
the following will work.

SELECT gl_detail.gl_year, gl_amount,

case
when gl_amount > 0 then gl_amount
else 0
end as Debit,
case
when gl_amount < 0 then gl_amount * -1
else 0
end as credit
FROM gl_detail

Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Code:
SELECT gl_detail.gl_year, 
       gl_amount,
       case when gl_amount > 0 
            then gl_amount
            else 0
            end As Debit,
       case when gl_amount < 0 
            then gl_amount * -1
            else 0 
            end As Credit
FROM gl_detail

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top