2016-11-30 84 views
0

我有一個查詢,我需要找到一個數值類似於「00627987800001」的refnum的值,所以當我正在編寫查詢時,我面臨兩個問題。如何在Oracle中使用蒙版

1.我需要包括最初的零的完整數字。

2.Another關心的是我需要使用作出數 - 分離像0062-7987-800-001

和我的查詢如下。

SELECT nvl(to_char((SELECT lr.location_refnum_value 
        FROM location_refnum lr, 
          servprov  sr, 
          location  loc, 
          shipment  sh 
        WHERE sh.servprov_gid = sr.servprov_gid 
         AND sr.servprov_gid = loc.location_gid 
         AND loc.location_gid = lr.location_gid 
         AND lr.location_refnum_qual_gid = 'ULA.UL_CPF' 
         AND sh.shipment_gid = :p_shipment_id), 
        '999999999999'), 
      '-') 
    FROM dual 
+0

SELECT NVL(TO_CHAR((SELECT LR.LOCATION_REFNUM_VALUE FROM LOCATION_REFNUM LR,SR SERVPROV,LOCATION LOC,裝運SH WHERE SH.SERVPROV_GID = SR.SERVPROV_GID AND SR.SERVPROV_GID = LOC.LOCATION_GID AND LOC.LOCATION_GID = LR.LOCATION_GID AND LR.LOCATION_REFNUM_QUAL_GID ='ULA.UL_CPF' AND SH.SHIPMENT_GID =:P_SHIPMENT_ID),'999999999999'),' - ')FROM DUAL –

回答

0
select regexp_replace (to_char (627987800001,'FM00000000000000'),'(.{4})(.{4})(.{3})(.{3})','\1-\2-\3-\4') 

from dual 
; 

...並通過各種手段,使用ANSI/ISO的JOIN

select regexp_replace 
     (
      to_char (lr.location_refnum_value,'FM00000000000000') 
      ,'(.{4})(.{4})(.{3})(.{3})' 
      ,'\1-\2-\3-\4' 
     ) 

from   location_refnum   lr 

     join location    loc 

     on  loc.location_gid = 
       lr.location_gid 

     join servprov    sr 

     on  sr.servprov_gid  = 
       loc.location_gid 

     join shipment    sh 

     on  sh.servprov_gid  = 
       sr.servprov_gid 

where lr.location_refnum_qual_gid = 'ULA.UL_CPF' 
    and sh.shipment_gid    = :p_shipment_id 
; 
0
  1. 您可以使用TO_NUMBER函數將字符串轉換成相應的 「數」價值在您的where條件進一步比較。

  2. 您可以使用instrsubstr函數的組合來在所給出的數字之間附加所需的字符「 - 」。

編輯

  • 替代地可以使用正則表達式如嘟嘟在他的回答建議。