Oracle SQL If Statement

Is it possible to use a if statement in a oracle sql query ??

I am trying to implement something like

select if (ADDRESS.SYS_TM_STMP = null) then TO_CHAR(ADDRESS.SYS_TM_STMP, 'yyyy-mm-dd') || 'T' || TO_CHAR(ADDRESS.SYS_TM_STMP, 'hh24:mi:ss') SYS_TM_STMP else (SYS_TM_STMP) from organization where orgid='10'

LVL 21
surajgupthaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

boriskalavskyCommented:
0
surajgupthaAuthor Commented:
Yeah i can use Case to solve the problem. I am wondering if there is an IF, if so how to use that.
0
earth man2Commented:
select replace( to_char( nvl(  ADDRESS.SYS_TM_STMP , SYSDATE ),  'yyyy-mm-dd hh24:mi:ss', ' ', 'T') ) AS SYS_TM_STMP from organization where orgid='10'
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

kumaran100Commented:
Hi ,
Here is the solution........  Hope this will Help You

1.
select case  when  ADDRESS.SYS_TM_STMP is null then
                          TO_CHAR(ADDRESS.SYS_TM_STMP, 'yyyy-mm-dd') || 'T' ||
                                 TO_CHAR(ADDRESS.SYS_TM_STMP, 'hh24:mi:ss') SYS_TM_STMP
                   else
                           SYS_TM_STMP
                   end
from            organization
where          orgid = '10';

2.
select           decode( ADDRESS.SYS_TM_STMP , NULL  ,
                          TO_CHAR(ADDRESS.SYS_TM_STMP, 'yyyy-mm-dd') || 'T' ||
                          TO_CHAR(ADDRESS.SYS_TM_STMP, 'hh24:mi:ss')  ,  SYS_TM_STMP                  
                   end
from            organization
where          orgid = '10';




Rdgs,
kums






0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kumaran100Commented:
Hi,


1.
select case  when  ADDRESS.SYS_TM_STMP is null then
                          TO_CHAR(ADDRESS.SYS_TM_STMP, 'yyyy-mm-dd') || 'T' ||
                                 TO_CHAR(ADDRESS.SYS_TM_STMP, 'hh24:mi:ss')                  
                    else
                           SYS_TM_STMP
                   end
from            organization
where          orgid = '10';

2.
select           decode( ADDRESS.SYS_TM_STMP , NULL  ,
                          TO_CHAR(ADDRESS.SYS_TM_STMP, 'yyyy-mm-dd') || 'T' ||
                          TO_CHAR(ADDRESS.SYS_TM_STMP, 'hh24:mi:ss')  ,  SYS_TM_STMP                  
                   end
from            organization
where          orgid = '10';




Rdgs,
kums
0
boriskalavskyCommented:
you can use case or decode.
0
randydCommented:
case or decode is the typical response to this type of question - earthman2 shows the SQL solution without resorting to the recent more programmatic extensions.

i think there is almost always a sql solution without the need for an IF.
0
SMartinHamburgCommented:
I you really want to use IF you can go for PLSQL. But unless you have samething really
special in mind and your example is just an over-simplification I see no good reason
why who would want to do this.
Kind Regards
0
surajgupthaAuthor Commented:
Kumaran100,
                  When i run that query it says missing keyword. Please let me know what to do.
0
JankovskyCommented:
Teh described functionality you can solve such way:

select NVL(SYS_TM_STMP,TO_CHAR(ADDRESS.SYS_TM_STMP, 'yyyy-mm-dd') || 'T' || TO_CHAR(ADDRESS.SYS_TM_STMP, 'hh24:mi:ss')) from organization where orgid='10'

The statement could work just if there is a package ADDRESS.
But I affraid there shoud be some join of the ADDRESS table in the statement, like this:

select NVL(organization.SYS_TM_STMP,TO_CHAR(ADDRESS.SYS_TM_STMP, 'yyyy-mm-dd') || 'T' || TO_CHAR(ADDRESS.SYS_TM_STMP, 'hh24:mi:ss')) from organization join address on(address.<key>=organization.<key> )
where orgid='10'
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

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.