【发布时间】:2019-11-19 17:42:37
【问题描述】:
我有如下字符串:
Geographical Information & Income: Income - National Classifications: Los Angeles - Low
你知道怎么只能得到“低”吗?这对我来说很难,因为我不知道如何告诉子字符串从第二个“-”开始。
【问题讨论】:
标签: sql sql-server substring
我有如下字符串:
Geographical Information & Income: Income - National Classifications: Los Angeles - Low
你知道怎么只能得到“低”吗?这对我来说很难,因为我不知道如何告诉子字符串从第二个“-”开始。
【问题讨论】:
标签: sql sql-server substring
对于这个字符串,使用这样的字符串函数:
declare @s varchar(100) = 'Geographical Information & Income: Income - National Classifications: Los Angeles - Low';
select ltrim(right(@s, charindex('-', reverse(@s)) - 1))
【讨论】:
你可以使用RIGHT得到最右边的词:
DECLARE @string NVARCHAR(500) = 'Geographical Information & Income: Income - National Classifications: Los Angeles - Low'
SELECT RIGHT(@string,CHARINDEX(' ',REVERSE(@string)) - 1)
【讨论】:
我建议结合使用修剪:
select trim(RIGHT ( 'Geographical Information & Income: Income - National Classifications: Los Angeles - Low ' , charindex('-', reverse('Geographical Information & Income: Income - National Classifications: Los Angeles - Low '))-1) )
为什么?好吧,因为你可以有这样的案例 'test - test - word ' 干杯!
【讨论】:
这对“-”的第二个索引有效
Select
Substr(
Instr(
Substr(
Instr(Substr(string, "-")+1,
Length(string)
),
"-")+1 ,
length(string)) from table;
【讨论】: