Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select query select with Substr returns NULL

I am trying to query the name field in the Oracle DB like below

select NAME, Substr(NAME,1,instr(NAME,' ') - 1) as SHORTNAME from rm_room

what I am trying is if the Name field has space between the string the select only charcters on the left of the space. I am expecting the above query to return

NAME SHORTNAME
AX1 BD AX1
A1 A1
BC W1 BC

but the issue is if the name field doesnot have space then it is returning null in the SHORTNAME Field

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

NAME SHORTNAME
AX1 BD AX1
A1 NULL
BC W1 BC

Can we use if condition in the select clause if so how can I check that

>Solution :

you could just use the NVL function which sets a default value instead of null

select NAME, NVL(Substr(NAME,1,instr(NAME,' ') - 1),NAME) as SHORTNAME from rm_room
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading