如何在SQL DB2中的下一行中转换子字符串

时间:2017-10-06 09:42:57

标签: sql db2

我有一个带有子字符串的列 我想使用db2 sql在下一行' - '之后移动子字符串 我怎么能这样做?我想在一列或多列上做同样的事情  有人对此有任何解决方案吗?

col1

|abc_pqr|
|lmn_abc|

输出

|abc|
|pqr|
|lmn|
|abc|

这是我的查询

(SELECT DISTINCT T1.PROJECT_NAME,
       T1.REFERENCE_ID,
       T1.NAME AS URL1_title,
       T1.URL AS URL1,
       T1.REQUEST_TYPE,
       T2.VAL||'_'||T3.VAL AS  value1

FROM RIDW.VW_REQUEST T1
LEFT OUTER JOIN RICALM.VW_RQST_STRING_EXT T2
ON T2.REQUEST_ID=T1.REQUEST_ID AND T2.NAME='owner_1'
  LEFT OUTER JOIN RICALM.VW_RQST_STRING_EXT T3
  ON T3.REQUEST_ID=T1.REQUEST_ID AND T3.NAME='owner_2'
WHERE T1.PROJECT_ID = 0  AND
(  T1.REQUEST_TYPE = 'Task' 
) AND
(T1.ISSOFTDELETED = 0) AND
(T1.REQUEST_ID <> -1 AND T1.REQUEST_ID IS NOT NULL))a

union all
select substr(a.value1,locate(a.value1,'_')+1) from 

(SELECT DISTINCT T1.PROJECT_NAME,
       T1.REFERENCE_ID,
       T1.NAME AS URL1_title,
       T1.URL AS URL1,
       T1.REQUEST_TYPE,
       T2.VAL||'_'||T3.VAL AS  value1

FROM RIDW.VW_REQUEST T1
LEFT OUTER JOIN RICALM.VW_RQST_STRING_EXT T2
ON T2.REQUEST_ID=T1.REQUEST_ID AND T2.NAME='owner_1'
  LEFT OUTER JOIN RICALM.VW_RQST_STRING_EXT T3
  ON T3.REQUEST_ID=T1.REQUEST_ID AND T3.NAME='owner_2'
WHERE T1.PROJECT_ID = 0  AND
(  T1.REQUEST_TYPE = 'Task' 
) AND
(T1.ISSOFTDELETED = 0) AND
(T1.REQUEST_ID <> -1 AND T1.REQUEST_ID IS NOT NULL))a

我知道我错过了上述查询中的内容

1 个答案:

答案 0 :(得分:1)

select substr(a.o1,1,LOCATE_IN_STRING(a.o1,'_')-1) as qwert from 

(SELECT DISTINCT T1.PROJECT_NAME,
       T1.REFERENCE_ID,
       T1.NAME AS URL1_title,
       T1.URL AS URL1,
       T1.REQUEST_TYPE,
       T2.VAL||'_' ||T3.VAL||'_'|| T4.VAL AS o1     
FROM RIDW.VW_REQUEST T1
LEFT OUTER JOIN RICALM.VW_RQST_STRING_EXT T2
ON T2.REQUEST_ID=T1.REQUEST_ID AND T2.NAME='owner_1'
  LEFT OUTER JOIN RICALM.VW_RQST_STRING_EXT T3
  ON T3.REQUEST_ID=T1.REQUEST_ID AND T3.NAME='owner_2'
    LEFT OUTER JOIN RICALM.VW_RQST_STRING_EXT T4
    ON T4.REQUEST_ID=T1.REQUEST_ID AND T4.NAME='owner_3'
WHERE T1.PROJECT_ID = 0  AND
(  T1.REQUEST_TYPE = 'Task' 
) AND
(T1.ISSOFTDELETED = 0) AND
(T1.REQUEST_ID <> -1 AND T1.REQUEST_ID IS NOT NULL))a

union all

select substr(b.o1,LOCATE_IN_STRING(b.o1,'_')+1,LOCATE_IN_STRING(b.o1,'_')-1) as qwert from 
(SELECT DISTINCT T1.PROJECT_NAME,
       T1.REFERENCE_ID,
       T1.NAME AS URL1_title,
       T1.URL AS URL1,
       T1.REQUEST_TYPE,
              T2.VAL||'_'||T3.VAL||'_'||T4.VAL AS o1    
FROM RIDW.VW_REQUEST T1
LEFT OUTER JOIN RICALM.VW_RQST_STRING_EXT T2
ON T2.REQUEST_ID=T1.REQUEST_ID AND T2.NAME='owner_1'
  LEFT OUTER JOIN RICALM.VW_RQST_STRING_EXT T3
  ON T3.REQUEST_ID=T1.REQUEST_ID AND T3.NAME='owner_2'
    LEFT OUTER JOIN RICALM.VW_RQST_STRING_EXT T4
    ON T4.REQUEST_ID=T1.REQUEST_ID AND T4.NAME='owner_3'
WHERE T1.PROJECT_ID = 0  AND
(  T1.REQUEST_TYPE = 'Task' 
) AND
(T1.ISSOFTDELETED = 0) AND
(T1.REQUEST_ID <> -1 AND T1.REQUEST_ID IS NOT NULL))b

union all

select substr(c.o1,locate('_',c.o1,2)+1,locate('_',c.o1,2)-1)  as qwert from 

(SELECT DISTINCT T1.PROJECT_NAME,
       T1.REFERENCE_ID,
       T1.NAME AS URL1_title,
       T1.URL AS URL1,
       T1.REQUEST_TYPE,
       T2.VAL||'_' ||T3.VAL||'_'|| T4.VAL AS o1     
FROM RIDW.VW_REQUEST T1
LEFT OUTER JOIN RICALM.VW_RQST_STRING_EXT T2
ON T2.REQUEST_ID=T1.REQUEST_ID AND T2.NAME='owner_1'
  LEFT OUTER JOIN RICALM.VW_RQST_STRING_EXT T3
  ON T3.REQUEST_ID=T1.REQUEST_ID AND T3.NAME='owner_2'
    LEFT OUTER JOIN RICALM.VW_RQST_STRING_EXT T4
    ON T4.REQUEST_ID=T1.REQUEST_ID AND T4.NAME='owner_3'
WHERE T1.PROJECT_ID = 0  AND
(  T1.REQUEST_TYPE = 'Task' 
) AND
(T1.ISSOFTDELETED = 0) AND
(T1.REQUEST_ID <> -1 AND T1.REQUEST_ID IS NOT NULL))c

这是我修改后的查询

例如

   select substr('abc_pqr_rams',1,LOCATE_IN_STRING('abc_pqr_rams','_')-1) from dual
    union all
    select substr('abc_pqr_rams',LOCATE_IN_STRING( 'abc_pqr_rams','_' )+1,LOCATE_IN_STRING('abc_pqr_rams','_')-1) from dual
    union all
    select substr('abc_pqr_rams',LOCATE_IN_STRING('abc_pqr_rams','_',1,2)+1,LOCATE_IN_STRING('abc_pqr_rams','_',1,2)-1) from dual