Q
Problem solve Get help with specific problems with your technologies, process and projects.

Conditional values in the SELECT list

Need to test on a field to determine the value that is displayed in a result set. Can I do that in the SELECT statement?

select memb.acct, memb.transamt, memb.name
if memb.transactioncode < 100 then memb.transamt = '0' 
else memb.transactioncode = '100'

I realise that the syntax is not correct, but this is what I am trying to accomplish.


SQL includes the CASE statement, a powerful conditional structure in the SELECT list; however, not all databases support this yet.

I think there's a small error in your question; you are setting the transaction amount to 0 when the transaction code is less than 100, otherwise you are setting the transaction code to 100. I'm going to change this slightly just to illustrate the syntax --

select memb.acct
     , CASE
         WHEN memb.transactioncode < 100
         THEN 0
         ELSE 100
       END  as transamt
     , memb.name
  from memb

See also this previous answer for an example of conditional logic using the Microsoft Access Iif function.

For More Information


Dig Deeper on Oracle and SQL

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchHRSoftware

Close