How to split a varchar column as multiple values in SQL?(如何在 SQL 中将 varchar 列拆分为多个值?)
问题描述
我有这个 SQL Select 语句
SELECTAD_Ref_List.ValueFROM AD_Ref_ListWHERE AD_Ref_List.AD_Reference_ID= 1000448
这是SELECT的结果:
为了限制选定的行,我在其他表中存储了几个值,如下所示:
SELECT xx_insert.XX_DocAction_Next从 xx_insert哪里 xx_insert_id = 1000283
所以,我最后的 SQL Select 是这样的:
SELECTAD_Ref_List.ValueFROM AD_Ref_ListWHERE AD_Ref_List.AD_Reference_ID= 1000448AND AD_Ref_List.Value IN(选择 xx_insert.XX_DocAction_Next从 xx_insert哪里 xx_insert_id = 1000283);
问题:这个 SELECT 没有返回任何行,因为 Oracle 已经转换成这样:AD_Ref_List.Value IN ('CO,VO')
但是,我需要的是:AD_Ref_List.Value IN ('CO','VO')
我该怎么做???
最好的问候
将你在分隔列表中使用的分隔符中的值包裹起来,然后检查它是否是分隔列表的子字符串(也包括分隔符包裹围绕它):
SELECT r.ValueFROM AD_Ref_List r内连接 xx_insert xON ( ',' || x.XX_DocAction_Next || ',' LIKE '%,' || r.value || ',%' )WHERE r.AD_Reference_ID = 1000448AND x.xx_insert_id = 1000283;
<块引用>
我必须在 whereClause 中保留逻辑
真的,不要.上面的查询会更有效率.
但如果你必须这样做:
SELECT 值FROM AD_Ref_ListWHERE AD_Reference_ID = 1000448与值输入 (SELECT REGEXP_SUBSTR( XX_DocAction_Next, '[^,]+', 1, LEVEL )从 xx_insert哪里 xx_insert_id = 1000283按级别连接 <= REGEXP_COUNT( XX_DocAction_Next, '[^,]+' ));
I have this SQL Select Statement
SELECT
AD_Ref_List.Value
FROM AD_Ref_List
WHERE AD_Ref_List.AD_Reference_ID= 1000448
This is the result of SELECT:
To limit the selected rows, I have a couple of values stored in other table like this:
SELECT xx_insert.XX_DocAction_Next
FROM xx_insert
WHERE xx_insert_id = 1000283
So, My final SQL Select is this:
SELECT
AD_Ref_List.Value
FROM AD_Ref_List
WHERE AD_Ref_List.AD_Reference_ID= 1000448
AND AD_Ref_List.Value IN
(SELECT xx_insert.XX_DocAction_Next
FROM xx_insert
WHERE xx_insert_id = 1000283
)
;
PROBLEM : This SELECT return no line, because Oracle has transformed like this: AD_Ref_List.Value IN ('CO,VO')
But, what I need is : AD_Ref_List.Value IN ('CO','VO')
How can I do this???
Best regards
Wrap the value in the delimiter you are using in the delimited list and then check if it is a sub-string of the delimited list (also with the delimiters wrapped around it):
SELECT r.Value
FROM AD_Ref_List r
INNER JOIN xx_insert x
ON ( ',' || x.XX_DocAction_Next || ',' LIKE '%,' || r.value || ',%' )
WHERE r.AD_Reference_ID = 1000448
AND x.xx_insert_id = 1000283;
i must keep the logic in the whereClause
Really, don't. The above query will be much more efficient.
But if you have to then:
SELECT Value
FROM AD_Ref_List
WHERE AD_Reference_ID = 1000448
AND value IN (
SELECT REGEXP_SUBSTR( XX_DocAction_Next, '[^,]+', 1, LEVEL )
FROM xx_insert
WHERE xx_insert_id = 1000283
CONNECT BY LEVEL <= REGEXP_COUNT( XX_DocAction_Next, '[^,]+' )
);
这篇关于如何在 SQL 中将 varchar 列拆分为多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 SQL 中将 varchar 列拆分为多个值?
基础教程推荐
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01