[英]Seperate data into different columns in Oracle Sql developer
Table_1 包含具有這些屬性的消息
消息 -> VARCHAR2(2000 BYTE) -> 可為空:是
消息由新行分隔,始終為“CLRF”。
表_1 消息John DoeCRLF555-555-5555CRLFThis is a test message
我怎樣才能把它們分成不同的列
幾件事:
chr(13)||chr(10)
id
)。 同樣,請參閱上面的鏈接,了解一個非常明確的問題的一個很好的例子。無論如何,這是將換行符分隔的字符串拆分為 ROWS 的常用方法。 這是大多數人所做的。 正則表達式是我在這里根據您的情況更改的唯一部分(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;
如果您想將其拆分為 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
;
我相信你正在尋找這樣的東西。 需要 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.
您可以使用SUBSTRING_INDEX
方法。 請參閱下面的獨立示例:
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.