Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract data expression 1

Status
Not open for further replies.

kernal

Technical User
Feb 27, 2001
415
US
I have 2 fields that I need to use in an expression:

Address Emerg_Contact
C/O John blank
C/O Sam null
C/O Jane Jones,Sam

Expression needed:

Emerg_Contact has data then take the data after the comma, add a space then take the data before the comma so Jones, Sam becomes Sam Jones, etc.

else if

emerg_contact is null or blank and Address has "C/O" then take the next character it finds after the "C/O" in the Address so the first record it would retrieve John. Note: I mentioned next character after the "C/O" becomes sometimes more than 1 space is entered after the "C/O"

Help is very appreciated.
 
Here is some code:
Code:
SQL> select address, '['||emerg_contact||']' from mytab;

ADDRESS    '['||EMERG_C
---------- ------------
C/O John   [ ]
C/O Sam    []
C/O Jane   [Jones,Sam]

select case when ltrim(emerg_contact) is null then replace(Address,'C/O ')
            when instr(emerg_contact,',')>0   then 
                 substr(emerg_contact,instr(emerg_contact,',')+1) ||' '||
                 substr(emerg_contact,1,instr(emerg_contact,',')-1)
       end result
  from mytab;

RESULT
---------------------
John
Sam
Sam Jones
Let us know if this is what you wanted.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
“Beware of those that seek to protect you from harm or risk. The cost will be your freedoms and your liberty.”
 
thanks dave. I'll send another post as soon as I'm able to try it.
 
Kernal,

Prior to more rigorous testing, I propose adding one more line of code to my previous SQL. The way it was, nothing displays if the EMERG_CONTACT is neither blank, nor null, nor contains a comma. This code resolves that issue:
Code:
SQL> select * from mytab;

ADDRESS    EMERG_CONT
---------- ----------
C/O John
C/O Sam
C/O Jane   Jones,Sam
Whatever   NYC Police

4 rows selected.

select case when ltrim(emerg_contact) is null then replace(Address,'C/O ')
            when instr(emerg_contact,',')>0   then 
                 substr(emerg_contact,instr(emerg_contact,',')+1) ||' '||
                 substr(emerg_contact,1,instr(emerg_contact,',')-1)
            else emerg_contact
       end result
  from mytab;

RESULT
---------------------
John
Sam
Sam Jones
NYC Police

4 rows selected.
Let us know how this code works for you.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
“Beware of those that seek to protect you from harm or risk. The cost will be your freedoms and your liberty.”
 
BTW, Kernal, are you somewhere on the Wasatch Front?

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
“Beware of those that seek to protect you from harm or risk. The cost will be your freedoms and your liberty.”
 
It works WONDERFUL! Thank you so much. I live in SLC and glad the micro bursts have stopped.
 
I'm glad it works for you, Kernal. (Thanks for the little purple
star.gif
, as well.)

I work downtown in SLC and live in Sandy. If you'd like to get together for our own Mini (Root)Beery Meet, then let me know via my signature, below.

Regards,


[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
“Beware of those that seek to protect you from harm or risk. The cost will be your freedoms and your liberty.”
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top