如何在Oracle中获取子字符串索引

时间:2018-05-17 07:56:33

标签: sql database oracle function oracle11g

我的数据库(Oracle)中有以下字段

Jayson,1990,3,july

我想在这里得到所有的价值观。 会有什么解决方案?

3 个答案:

答案 0 :(得分:1)

mkdir -p /quota
mkdir -p /var/virtual_disks
touch /var/virtual_disks/directory_with_size_limit.ext3
dd if=/dev/zero of=/var/virtual_disks/directory_with_size_limit.ext3 count=51200
mkfs.ext3 /var/virtual_disks/directory_with_size_limit.ext3
mount -o loop,rw,usrquota,grpquota /var/virtual_disks/directory_with_size_limit.ext3 /quota

enter image description here

答案 1 :(得分:0)

  

我想在这里得到所有的值

"在这里"?

也许是这样的事情?

SQL> with test (col) as
  2    (select 'Jayson,1990,3,july' from dual)
  3  select regexp_substr(col, '[^,]+', 1, level) result
  4  from test
  5  connect by level <= regexp_count(col, ',') + 1;

RESULT
------------------
Jayson
1990
3
july

SQL>

[编辑,看到评论后]

两个简单选项:一个是使用REGEXP_SUBSTR,另一个是使用传统的SUBSTR + INSTR组合:

SQL> with test (col) as
  2    (select 'Jayson,1990,3,july' from dual)
  3  select
  4    regexp_substr(col, '\w+', 1, 1) name,
  5    regexp_substr(col, '\w+', 1, 2) year,
  6    regexp_substr(col, '\w+', 1, 3) day,
  7    regexp_substr(col, '\w+', 1, 1) month,
  8    --
  9    substr(col, 1, instr(col, ',', 1, 1) - 1) name_2,
 10    substr(col, instr(col, ',', 1, 1) + 1, instr(col, ',', 1, 2) - instr(col, ',', 1, 1) - 1) year_2,
 11    substr(col, instr(col, ',', 1, 2) + 1, instr(col, ',', 1, 3) - instr(col, ',', 1, 2) - 1) day_2,
 12    substr(col, instr(col, ',', 1, 3) + 1) month_2
 13  from test;

NAME   YEAR D MONTH  NAME_2 YEAR D MONT
------ ---- - ------ ------ ---- - ----
Jayson 1990 3 Jayson Jayson 1990 3 july

SQL>

答案 2 :(得分:0)

抱歉描述不好。 我想首先获得FirstName的第一个名称,然后将日期作为Date,然后将日期作为Day。所以我想每次返回一行。我将执行4次以检索所有结果

结果:

Name:
Jayson

Date:
 1990

Day:
3