第二次出现后的SQL子字符串

问题描述 投票:0回答:4

我有如下字符串:

Geographical Information & Income: Income - National Classifications: Los Angeles - Low

您知道如何只能从中获得“低”吗?这对我来说很难,因为我不知道如何告诉子字符串从第二个“-”开始。

sql sql-server substring
4个回答
0
投票

对于此字符串,请使用如下字符串函数:

declare @s varchar(100) = 'Geographical Information & Income: Income - National Classifications: Los Angeles - Low';

select ltrim(right(@s, charindex('-', reverse(@s)) - 1))

0
投票

您可以使用RIGHT获得最右边的单词:

DECLARE @string NVARCHAR(500) = 'Geographical Information & Income: Income - National Classifications: Los Angeles - Low'

SELECT RIGHT(@string,CHARINDEX(' ',REVERSE(@string)) - 1)

0
投票

我建议结合使用修剪:

select trim(RIGHT ( 'Geographical Information & Income: Income - National Classifications: Los Angeles - Low ' , charindex('-', reverse('Geographical Information & Income: Income - National Classifications: Los Angeles - Low '))-1) )

为什么?好吧,因为您可以遇到类似“测试-测试-单词”的情况干杯!


0
投票

这对于“-”的第二索引有效

     Select
            Substr(
          Instr(
                Substr(
                Instr(Substr(string, "-")+1, 
                Length(string)
                            ), 
            "-")+1 , 
            length(string)) from table;
© www.soinside.com 2019 - 2024. All rights reserved.