简体   繁体   English

在 Oracle Sql developer 中将数据分成不同的列

[英]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我怎样才能把它们分成不同的列

  1. Column_1: Message Column_1:消息
  2. Column_2: John Doe Column_2:李四
  3. Column_3: 555-555-5555 Column_3:555-555-5555
  4. Column_4: This is a test message. Column_4:这是一条测试消息。

A couple things:几件事:

  1. This sounds like a version of the common question " how do I split a comma-separated string ", but with CRLF instead of commas.这听起来像是常见问题“ 如何拆分以逗号分隔的字符串”的一个版本,但使用的是 CRLF 而不是逗号。 The answers on that link are great, so be sure to look at those.该链接上的答案很好,所以一定要看看那些。
  2. You aren't really clear about whether your separator is the string "CRLF" or an actual carriage return-line feed combination.您不太清楚您的分隔符是字符串“CRLF”还是实际的回车换行符组合。 I'm going to guess it's the second one, which in Oracle can be represented as chr(13)||chr(10)我猜是第二个,在 Oracle 中可以表示为chr(13)||chr(10)
  3. The simple answer (below) will only really work for a single input row.简单的答案(如下)仅适用于单个输入行。 If you want this to work on multiple rows, you'll have to tell us what primary key you're using for this table (eg 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM