简体   繁体   中英

sql separators (DB2 but ORACLE can be too)

I need help with separators in sql. I'm working on DB2 but Oracle is also good.

I need to build query where I've got data in format: aaa.bbb.ccc.ddd@domain.com where 'aaa', 'bbb', 'ccc', 'ddd' got not constant length. Query should return bbb and ddd. In DB2 I can cut '@domain.com' which takes me really long line. Rest I have no idea how to move. I tried with SUBSTR but nothing has work like it should nad my queries are super long.

I need query not block. EXAMPLE: data in column:

John.W.Smith.JWS1@domain.com
Alexia.Nova.Alnov@domain.com
Martha.Heart.Martha2@domain.com

etc.

In general I need to get data from between 1st and 2nd separator . and the one which is in front of @ .

I'm sure someone will have some clever REGEX way of doing it, but this is one way to do it.

with test as
( select 'John.W.Smith.JWS1@domain.com' col1 from dual union all
select 'Alexia.Nova.Alnov@domain.com' from dual union all
select 'Martha.Heart.Martha2@domain.com' from dual
)
select col1
        , substr( col1, 1, dot_one-1 ) f1
        , substr( col1, dot_one+1, dot_two - dot_one -1 ) f2
        , no_domain
        , substr( no_domain, dot_before_at+1 ) f3
from
(
select col1
        ,instr( col1, '@', -1 ) at_pos
        ,instr( col1, '.',1,1) dot_one
        ,instr( col1, '.',1,2) dot_two
        ,substr( col1, 1, instr(col1, '@', -1 )-1) no_domain
        ,instr( substr( col1, 1, instr( col1, '@', -1 ) -1 ) , '.', -1 ) dot_before_at
from test
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM