Oracle SQL:如何使用 to_char 转换字符串中的子字符串

Oracle SQL: How do I convert the substrings in a string using to_char

to_char 应用于如下所示的单个匹配项时效果很好。

select to_char('54523234', '99,999,999,999')

我使用正则表达式仅查找字符串中货币的匹配项。但是当我将 to_char 应用于第一组时,它不起作用

SELECT regexp_replace ('24444.88, 54523234.78, and 1044.52 are numbers in this example.', 
'(([0-9]+){1,10})+\.([0-9]{2})', to_char(''.'99,999,999,999'))
FROM dual;

我希望输出字符串显示如下: 24,444.8854,523,234.781,044.52 是本例中的数字。

给你兄弟,这很有趣:

SELECT regexp_replace(regexp_replace ('234234234.88, 24444.88, 54523234.78, and 1044.52 are numbers in this example.', 
'(([0-9]{3})\.)',
','),'(([0-9]{3}),)',',')
FROM dual

SQL Fiddle

Oracle 11g R2 架构设置:

CREATE OR REPLACE FUNCTION reverseString(
  str VARCHAR2
) RETURN VARCHAR2
AS
  v_str VARCHAR2(4000);
BEGIN
  FOR i IN REVERSE 1 .. LENGTH( str ) LOOP
    v_str := v_str || SUBSTR( str, i, 1 );
  END LOOP;
  RETURN v_str;
END;
/

CREATE OR REPLACE FUNCTION insertDecimalSeparators(
  str VARCHAR2
) RETURN VARCHAR2
AS
  v_str VARCHAR2(4000) := str;
  v_pos NUMBER(4,0);
  v_num VARCHAR2(4000);
  v_rpl VARCHAR2(4000);
  c_re  CONSTANT CHAR(9) := '\d+\.?\d*';
BEGIN
  FOR i IN REVERSE 1 .. REGEXP_COUNT( str, c_re ) LOOP
    v_pos := REGEXP_INSTR( str, c_re, 1, i );
    v_num := REGEXP_SUBSTR( str, c_re, 1, i );
    v_rpl := REGEXP_REPLACE(
               reverseString( v_num ),
               '((\d*\.)?\d{3})',
               ',',
               1,
               0
             );
    IF SUBSTR( v_rpl, -1 ) = ',' THEN
      v_rpl := SUBSTR( v_rpl, 1, LENGTH( v_rpl ) - 1 );
    END IF;
    v_str := SUBSTR( v_str, 1, v_pos - 1 ) || reverseString( v_rpl ) || SUBSTR( v_str, v_pos + LENGTH( v_num ) );
  END LOOP;
  RETURN v_str;
END;
/

查询 1:

WITH data ( str ) AS (
            SELECT '244445.88, 54523234.78, and 1044.12345678 are numbers.' FROM DUAL
  UNION ALL SELECT 'abc123456' FROM DUAL
  UNION ALL SELECT '123, 12, 1234, 123.1' FROM DUAL
  UNION ALL SELECT '123.0123456789' FROM DUAL
)
SELECT str,
       insertDecimalSeparators( str ) AS commaSep
FROM   data

Results:

|                                                    STR |                                                   COMMASEP |
|--------------------------------------------------------|------------------------------------------------------------|
| 244445.88, 54523234.78, and 1044.12345678 are numbers. | 244,445.88, 54,523,234.78, and 1,044.12345678 are numbers. |
|                                              abc123456 |                                                 abc123,456 |
|                                   123, 12, 1234, 123.1 |                                      123, 12, 1,234, 123.1 |
|                                         123.0123456789 |                                             123.0123456789 |