tsql last “occurrence of” inside a string

后端 未结 1 1557
被撕碎了的回忆
被撕碎了的回忆 2021-01-17 10:11

I have got field containing comma separated values. I need to extract the last element in the list. I have tried with this:

select list_field, LTRIM(RTRIM(rig         


        
相关标签:
1条回答
  • 2021-01-17 10:26

    Find the last , by reversing the string and looking for the first occurrence, then read that many characters from the right of the string;

    rtrim(right(list_field, charindex(',', reverse(list_field)) - 1))
    

    (Use reverse(list_field) + ',' if there is the possibility of no delimiters in the field & you want the single value)

    0 讨论(0)
提交回复
热议问题