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
| 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