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

后端 未结 11 960
半阙折子戏
半阙折子戏 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 15:10

    @a - the value (f.e. 'a/bb/ccc/ffffdd/ee/ff/....')

    @p - the desired position (1,2,3...)

    @d - the delimeter ( '/' )

    trim(substring(replace(@a,@d,replicate(' ',len(@a))),(@p-1)*len(@a)+1,len(@a)))

    only problem is - if desired part has trailing or leading blanks they get trimmed.

    Completely Based on article from https://exceljet.net/formula/split-text-with-delimiter

提交回复
热议问题