Using T-SQL, return nth delimited element from a string

后端 未结 11 950
半阙折子戏
半阙折子戏 2020-11-22 14:45

I have a need to create a function the will return nth element of a delimited string.

For a data migration project, I am converting JSON audit records stored in a S

11条回答
  •  名媛妹妹
    2020-11-22 14:55

    You can use STRING_SPLIT with ROW_NUMBER:

    SELECT value, idx FROM
    (
      SELECT
        value,
        ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) idx
      FROM STRING_SPLIT('Lorem ipsum dolor sit amet.', ' ')
    ) t
    WHERE idx=2
    

    returns second element (idx=2): 'ipsum'

提交回复
热议问题