在regexp_replace中将大数字转换为Julian格式

时间:2013-03-11 22:46:52

标签: sql database oracle plsql

我只需要更换号码并保留其余部分。 例如:

34HUNTAVE ==> 34THHUNTAVE

1232432GRACE ST ==> 1232432NDGRACE ST

1232433GRACE ST ==> 1232433RDGRACE ST

问题是Julian格式在大数字上给出错误。我怎样才能使它工作。

   Does Not Work
    ------------
        SELECT REGEXP_REPLACE ('10425222GRACEST','[[:digit:]]+', 
         TO_CHAR (TO_DATE (REGEXP_REPLACE ('10425222GRACEST','[^[:digit:]]'),'J'),'fmJTH')) FROM dual;

     Works Fine
    ----------
         SELECT REGEXP_REPLACE ('10425BCST','[[:digit:]]+', 
         TO_CHAR (TO_DATE (REGEXP_REPLACE ('10425BCST','[^[:digit:]]'),'J'),'fmJTH')) FROM dual;

1 个答案:

答案 0 :(得分:2)

select 
  string,
  regexp_substr(string, '^\D*\d+') ||
  regexp_substr(to_char(to_date(
    regexp_replace(string, '^\D*\d*?(\d{1,3})(\D|$).*$', '100\1'), 
    'J'), 'fmjth'), '\D*$') ||
  regexp_replace(string, '^\D*\d+(.*)$', '\1') as new_string
FROM your_table

fiddle