SQL Server 中有 LastIndexOf 吗? [英] Is there a LastIndexOf in SQL Server?
本文介绍了SQL Server 中有 LastIndexOf 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从涉及获取 last index 的 字符串.目前,我正在做一个可怕的黑客,涉及反转字符串:
I am trying to parse out a value from a string that involves getting the last index of a string. Currently, I am doing a horrible hack that involves reversing a string:
SELECT REVERSE(SUBSTRING(REVERSE(DB_NAME()), 1,
CHARINDEX('_', REVERSE(DB_NAME()), 1) - 1))
对我来说这段代码几乎不可读.我刚刚升级到 SQL Server 2016,希望有更好的方法.有吗?
To me this code is nearly unreadable. I just upgraded to SQL Server 2016 and I hoping there is a better way. Is there?
推荐答案
如果你想要最后一个 _
之后的所有内容,那么使用:
If you want everything after the last _
, then use:
select right(db_name(), charindex('_', reverse(db_name()) + '_') - 1)
如果你想要之前的一切,那么使用left()
:
If you want everything before, then use left()
:
select left(db_name(), len(db_name()) - charindex('_', reverse(db_name()) + '_'))
这篇关于SQL Server 中有 LastIndexOf 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文