[英]How to get substring index in Oracle
I have the following field in my DB (Oracle) 我的数据库(Oracle)中有以下字段
Jayson,1990,3,july
And i would like to get all the values here. 我想在这里获取所有值。 What would be the solution?
解决办法是什么?
SELECT CASE WHEN rn = 1 THEN 'Name: '
WHEN rn =2 THEN 'Year: '
WHEN rn = 3 THEN 'Day: '
WHEN rn = 4 THEN 'Month: ' END || result "Results"
FROM
(
WITH TEST (col) AS
(SELECT 'Jayson,1990,3,july' FROM dual)
SELECT REGEXP_SUBSTR(col, '[^,]+', 1, LEVEL) result, ROWNUM rn
FROM TEST
connect BY LEVEL <= REGEXP_COUNT(col, ',') + 1
);
I would like to get all the values here
我想在这里获取所有值
Where is "here"? 这里是哪里”?
Something like this, perhaps? 大概是这样吗?
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>
[EDIT, after seeing the comment] [编辑,看到评论后]
Two simple options: one is to use REGEXP_SUBSTR
, another one is to use traditional SUBSTR
+ INSTR
combination: 两种简单的选择:一种是使用
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>
Sorry for bad description. 对不起,不好的描述。 I want to get firstly the first name as FirstName , then the date as Date ,then the day as Day.So i want to return me each time one row.I will execute it 4 times to retrive all results
我想首先获取名字为FirstName,然后将日期命名为Date,然后将日期命名为Day。所以我想每次返回一行。我将执行4次以检索所有结果
RESULT: 结果:
Name:
Jayson
Date:
1990
Day:
3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.