tsql 最后“发生"字符串内 [英] tsql last "occurrence of" inside a string

查看:23
本文介绍了tsql 最后“发生"字符串内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含逗号分隔值的字段.我需要提取列表中的最后一个元素.我试过这个:

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(right(list_field, len(list_field) - CHARINDEX(',',list_field))))

但它返回列表的最后一部分,从第一个逗号出现后开始.例如,

But it returns the last part of the list just starting after the first comma occurrence. For example,

a,b 返回 b

a,b,c 返回 b,c

a,b,c returns b,c

我想使用正则表达式之类的模式.是否可以在 TSQL(sql server 2008)中使用?还有什么线索吗?

I would like to use a regex like pattern. Is it possible in TSQL (sql server 2008)? Any other clues?

推荐答案

找到最后一个 ,;

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

(使用 reverse(list_field) + ',' 如果字段中可能没有分隔符并且您想要单个值)

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

这篇关于tsql 最后“发生"字符串内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆