[英]Seperate data into different columns in Oracle Sql developer
Table_1 contains messages that are of these attributes Table_1 包含具有这些属性的消息
message -> VARCHAR2(2000 BYTE) -> nullable: Yes消息 -> VARCHAR2(2000 BYTE) -> 可为空:是
Message are seperated by new line, "CLRF" always.消息由新行分隔,始终为“CLRF”。
Table_1 messages John DoeCRLF555-555-5555CRLFThis is a test message
表_1 消息
John DoeCRLF555-555-5555CRLFThis is a test message
How can I split them in separate columns我怎样才能把它们分成不同的列
A couple things:几件事:
chr(13)||chr(10)
chr(13)||chr(10)
id
).id
)。 Again, see the link above for a great example of a really clear question. Anyway, here's the usual way of splitting a newline-delimited string into ROWS.无论如何,这是将换行符分隔的字符串拆分为 ROWS 的常用方法。 This is what most people do.
这是大多数人所做的。 The regexp is the only part I changed here for your situation (CRLF instead of comma).
正则表达式是我在这里根据您的情况更改的唯一部分(CRLF 而不是逗号)。
-- sample data
with table_1 as (select 'John Doe' || chr(13)||chr(10) || '555-555-5555' || chr(13)||chr(10) || 'This is a test message' as message from dual)
-- query
select regexp_substr(message, '^[^'||chr(13)||']+', 1, level,'m')
from table_1
connect by regexp_substr(message, '^[^'||chr(13)||']+', 1, level,'m') is not null;
If you want to split it into COLUMNS, I don't think there's an easy way of generating a dynamic number of columns, so you'll have to manually set up each column.如果您想将其拆分为 COLUMNS,我认为没有一种简单的方法可以生成动态数量的列,因此您必须手动设置每一列。
-- sample data
with table_1 as (select 'John Doe' || chr(13)||chr(10) || '555-555-5555' || chr(13)||chr(10) || 'This is a test message' as message from dual)
-- query
select regexp_substr(message, '^[^'||chr(13)||']+', 1, 1,'m') as col1,
regexp_substr(message, '^[^'||chr(13)||']+', 1, 2,'m') as col2,
regexp_substr(message, '^[^'||chr(13)||']+', 1, 3,'m') as col3
from table_1
;
I believe you are looking for something like this.我相信你正在寻找这样的东西。 Expects 3 columns and allows for NULL elements.
需要 3 列并允许 NULL 个元素。
-- table_1 just sets up the test data
WITH table_1(message) AS (
SELECT 'John Doe'||CHR(13)||CHR(10)||'555-555-5555'||CHR(13)||CHR(10)||'John This is a test message' FROM dual UNION ALL
SELECT 'Jane Smith'||CHR(13)||CHR(10)||'555-555-1234'||CHR(13)||CHR(10)||'Jane This is a test message' FROM dual UNION ALL
SELECT 'Lance Link'||CHR(13)||CHR(10)||'555-555-1212'||CHR(13)||CHR(10)||'Lance This is a test message' FROM dual
)
SELECT message,
REGEXP_SUBSTR(message, '(.*?)('||CHR(13)||CHR(10)||'|$)', 1, LEVEL, NULL, 1) column_1,
REGEXP_SUBSTR(message, '(.*?)('||CHR(13)||CHR(10)||'|$)', 1, LEVEL+1, NULL, 1) column_2,
REGEXP_SUBSTR(message, '(.*?)('||CHR(13)||CHR(10)||'|$)', 1, LEVEL+2, NULL, 1) column_3
FROM table_1
CONNECT BY LEVEL <= REGEXP_COUNT(message, 'CHR(13)')+1
AND PRIOR message = message
AND PRIOR SYS_GUID() IS NOT NULL;
MESSAGE COLUMN_1 COLUMN_2 COLUMN_3
------------------------------ --------------- --------------- ------------------------------
Jane Smith
555-555-1234
Jane This is a test message Jane Smith 555-555-1234 Jane This is a test message
John Doe
555-555-5555
John This is a test message John Doe 555-555-5555 John This is a test message
Lance Link
555-555-1212
Lance This is a test message Lance Link 555-555-1212 Lance This is a test message
3 rows selected.
You can use SUBSTRING_INDEX
method.您可以使用
SUBSTRING_INDEX
方法。 See stand-alone example below:请参阅下面的独立示例:
SELECT
SUBSTRING_INDEX("John DoeCRLF555-555-5555CRLFThis is a test message","CRLF",1) as string1,
SUBSTRING_INDEX(SUBSTRING_INDEX("John DoeCRLF555-555-5555CRLFThis is a test message","CRLF",2),"CRLF",-1) as string2,
SUBSTRING_INDEX(SUBSTRING_INDEX("John DoeCRLF555-555-5555CRLFThis is a test message","CRLF",3),"CRLF",-1) as string3;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.