Returning values depending on data in column

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.
TechMonsterAsked:
Who is Participating?
 
sdstuberConnect With a Mentor Commented:
the NVL2 function does exactly the if null else check

Select col1, nvl2(col2,1,0) from table;
0
 
slightwv (䄆 Netminder) Commented:
Select col1, case when col2 is not null then 1 else 0 end mycol from table;
0
 
SharathData EngineerCommented:
Select col1, decode(col2,null,0,1) mycol from table;
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
slightwv (䄆 Netminder) Commented:
Decode is still valid but is being phased out.
0
 
TechMonsterAuthor Commented:
This is the solution I used...thanks
0
 
sdstuberCommented:
why the B?  that's a penalty grade,  what more did you want for an A?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.