将逗号替换为','

时间:2015-06-01 13:11:25

标签: oracle

编辑我的问题: -

我正在使用oracle的replace函数将,替换为',' 但是Oracle正在抛出两个函数参数的错误。

SELECT value
FROM person
WHERE value IN REPLACE(rakesh,satish, ',' ,'','');

rakesh和satish在我要检索的人员表中有一个主键。

有没有办法使用sql。

2 个答案:

答案 0 :(得分:2)

使用chr(39):

SELECT REPLACE('rakesh,satish', ',' ,chr(39)||','||chr(39))
FROM DUAL;

输出:

rakesh','satish

答案 1 :(得分:1)

回答问题的原始版本:

SQL Fiddle

如果您创建一个将列表拆分为集合的函数:

CREATE OR REPLACE FUNCTION splitList(
  str   VARCHAR2,
  delim VARCHAR2 DEFAULT ','
) RETURN SYS.ODCIVARCHAR2LIST DETERMINISTIC
AS
  o_list SYS.ODCIVARCHAR2LIST;
  p_prev NUMBER(4,0) := 1;
  p_next NUMBER(4,0);
BEGIN
  o_list := SYS.ODCIVARCHAR2LIST();
  IF ( str IS NULL ) THEN
    NULL;
  ELSIF ( delim IS NULL ) THEN
    o_list.EXTEND( 1 );
    o_list(1) := str;
  ELSE
    p_next := INSTR( str, delim, p_prev, 1 );
    WHILE ( p_next > 0 ) LOOP
      IF ( p_next > p_prev ) THEN
        o_list.EXTEND( 1 );
        o_list( o_list.COUNT ) := SUBSTR( str, p_prev, p_next - p_prev );
      END IF;
      p_prev := p_next + LENGTH( delim );
      p_next := INSTR( str, delim, p_prev, 1 );
    END LOOP;
    IF ( p_prev <= LENGTH( str ) ) THEN
      o_list.EXTEND( 1 );
      o_list( o_list.COUNT ) := SUBSTR( str, p_prev );
    END IF;
  END IF;
  RETURN o_list;
END splitList;
/

然后你可以这样做:

CREATE TABLE TableA ( primary_key, value ) AS
          SELECT 1, 'ab' FROM DUAL
UNION ALL SELECT 2, 'cd' FROM DUAL
UNION ALL SELECT 3, 'e' FROM DUAL
UNION ALL SELECT 4, 'f' FROM DUAL
UNION ALL SELECT 5, 'gh' FROM DUAL
/

CREATE TABLE TableB ( value ) AS
SELECT 'ab,cd,f' FROM DUAL
/

查询1

SELECT primary_key
FROM   TableA
WHERE  value IN ( SELECT COLUMN_VALUE
                  FROM   TABLE(
                           splitList(
                             ( SELECT value
                               FROM TableB
                             )
                           )
                         )
                 )

<强> Results

| PRIMARY_KEY |
|-------------|
|           1 |
|           2 |
|           4 |