【问题标题】:How to get the second word in a string using t-sql?如何使用 t-sql 获取字符串中的第二个单词?
【发布时间】:2020-05-25 16:44:44
【问题描述】:

我想从字符串或第一个和第三个空格之间的短语中提取第二个单词。

例如

“波音公司”

我想要“波音”

【问题讨论】:

    标签: string tsql parsing split charindex


    【解决方案1】:
    declare @sentence nvarchar(264);
    
    set @sentence = 'The Boeing Corporation';
    
    select ltrim(substring(@sentence,charindex(' ',@sentence), CHARINDEX(' ',ltrim(SUBSTRING(@sentence,charindex(' ',@sentence),LEN(@sentence)-charindex(' ',@sentence)))) ))
    
    | (无列名) | | :--------------- | |波音 |

    db小提琴here

    【讨论】:

      【解决方案2】:

      使用一点 XML 的另一种选择

      示例

      Declare @YourTable table (SomeCol varchar(500))
      Insert Into @YourTable values
      ('The Boeing Corporation')
      
      Select SomeCol
            ,Pos2 = cast('<x>' + replace(A.SomeCol,' ','</x><x>')+'</x>' as xml).value('/x[2]','varchar(50)')
       From  @YourTable A
      

      退货

      SomeCol                 Pos2
      The Boeing Corporation  Boeing
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-04
        相关资源
        最近更新 更多