Oracle计算varchar中的值多少

时间:2013-11-19 10:37:54

标签: oracle select count

我正在测试一个查询:

SELECT '-1,-2,-4,-6' "Verdunning"
  FROM DUAL

现在我需要知道varchar中有多少值:' - 1,-2,-4,-6'。我希望有4回。 当它是'-1,-2,-4,-6,-8'时,我需要回到5。如何在oracle select语句中执行此操作?

4 个答案:

答案 0 :(得分:2)

Fiddle

SELECT LENGTH(Verdunning) - LENGTH(REPLACE(Verdunning, ',', '')) + 1
FROM (SELECT '-1,-2,-4,-6' AS Verdunning FROM DUAL) T

答案 1 :(得分:1)

找到,的数量,然后加1以获得计数

Select LEN(column) – LEN(REPLACE(column, ',', ''))+1 as comma_separted_values_count
    From table

答案 2 :(得分:1)

试试这个:

SELECT
      REGEXP_COUNT ( REGEXP_REPLACE ( '-1,-2,-4,-6', '".*?"' ), ',' ) + 1
FROM
      DUAL;

答案 3 :(得分:0)

使用regexp_substr

select count(regexp_substr( '-1,-2,-4,-6','[^,]+', 1, level)) from dual
connect by regexp_substr( '-1,-2,-4,-6', '[^,]+', 1, level) is not null;