【问题标题】:SQL substring after second occurrence第二次出现后的 SQL 子字符串
【发布时间】:2019-11-19 17:42:37
【问题描述】:

我有如下字符串:

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

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

【问题讨论】:

    标签: sql sql-server substring


    【解决方案1】:

    对于这个字符串,使用这样的字符串函数:

    declare @s varchar(100) = 'Geographical Information & Income: Income - National Classifications: Los Angeles - Low';
    
    select ltrim(right(@s, charindex('-', reverse(@s)) - 1))
    

    【讨论】:

      【解决方案2】:

      你可以使用RIGHT得到最右边的词:

      DECLARE @string NVARCHAR(500) = 'Geographical Information & Income: Income - National Classifications: Los Angeles - Low'
      
      SELECT RIGHT(@string,CHARINDEX(' ',REVERSE(@string)) - 1)
      

      【讨论】:

        【解决方案3】:

        我建议结合使用修剪:

        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 ' 干杯!

        【讨论】:

          【解决方案4】:

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

               Select
                      Substr(
                    Instr(
                          Substr(
                          Instr(Substr(string, "-")+1, 
                          Length(string)
                                      ), 
                      "-")+1 , 
                      length(string)) from table;
          

          【讨论】:

            猜你喜欢
            • 2018-02-05
            • 2019-07-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-03-02
            • 2021-02-10
            • 2020-02-26
            • 2021-10-02
            相关资源
            最近更新 更多