Solved

Returning values depending on data in column

Posted on 2011-02-11
6
400 Views
Last Modified: 2012-05-11
I have a query in oracle..looking at returning a numeric value of 1 if a column is not NULL and 0.
some folks said that it would be similar to SQL but my query doesn't work.

So the value I want to change is Col2.  Set to 1 if the there is anything in Col2, and 0 if it is NULL.

Select
Col1,
If Col2 is NOT NULL THEN 1
Else 0 NewColName,
Col3,
From Database.
0
Comment
Question by:TechMonster
6 Comments
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
Select col1, case when col2 is not null then 1 else 0 end mycol from table;
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
Select col1, decode(col2,null,0,1) mycol from table;
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
Decode is still valid but is being phased out.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
Comment Utility
the NVL2 function does exactly the if null else check

Select col1, nvl2(col2,1,0) from table;
0
 

Author Closing Comment

by:TechMonster
Comment Utility
This is the solution I used...thanks
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
why the B?  that's a penalty grade,  what more did you want for an A?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now