[英]Convenient way to select 100+ columns (but not ALL the table columns) in SQL query at a time?
[英]SQL query to select a word from 100 columns
我在像这样Sh. North Miladhunmadulu - Shaviyani Atoll
的 Oracle 表中有 100 列Sh. North Miladhunmadulu - Shaviyani Atoll
Sh. North Miladhunmadulu - Shaviyani Atoll
。 我需要一个查询来从上面只选择Shaviyani
。 请帮我。 我需要后选择数据-
和之前Atoll
字。
假设您在谈论行而不是列。 您可以使用多个逗号分隔的 CTE 来实现这一点。
下面的查询有 3 个 cte 表。 表t0
将使用charindex
函数拉出'-'
的位置。 然后使用 charindex 函数我们可以找到应该在单词Shaviyani
之后的下一个空格。 正如您所看到的,在'-'
后面有一个空格。 因此,我们将向表t1
t0.position 添加 +2,用于查找下一个空间的位置。 表t3
将使用substring
函数获得最终结果。
declare @string nvarchar(50)='Sh. North Miladhunmadulu - Shaviyani Atoll';
with t0 AS (select charindex('-',@string) pos, @string name),
t1 AS (select charindex(' ',@string,pos+2) pos,name from t0),
t2 AS (select substring(@string,t0.pos+1,t1.pos - t0.pos) name
from t0
inner join t1 on t0.name = t1.name )
select * from t2
输出
name
--------------------------------------------------
Shaviyani
请使用SUBSTR
和INSTR
如下
SELECT SUBSTR('Sh. North Miladhunmadulu - Shaviyani Atoll',instr ('Sh. North Miladhunmadulu - Shaviyani Atoll','-')+1,
((instr ('Sh. North Miladhunmadulu - Shaviyani Atoll','Atoll')-1) - instr ('Sh. North Miladhunmadulu - Shaviyani Atoll','-'))) as OUTPUT
FROM dual;
这是Fiddle 的输出
用列名替换字符串,用表名替换dual
列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.