使用 T-SQL 查找子字符串最后一次出现的索引 [英] Find index of last occurrence of a sub-string using T-SQL
问题描述
是否有一种使用 SQL 查找字符串最后一次出现的索引的简单方法?我现在正在使用 SQL Server 2000.我基本上需要 .NET System.String.LastIndexOf
方法提供的功能.一点谷歌搜索揭示了这一点 - 检索最后索引的功能 - 但是如果您传入文本"列表达式,则不起作用.在别处找到的其他解决方案仅在您搜索的文本长度为 1 个字符时才有效.
Is there a straightforward way of finding the index of the last occurrence of a string using SQL? I am using SQL Server 2000 right now. I basically need the functionality that the .NET System.String.LastIndexOf
method provides. A little googling revealed this - Function To Retrieve Last Index - but that does not work if you pass in a "text" column expression. Other solutions found elsewhere work only so long as the text you are searching for is 1 character long.
我可能需要做一个功能.如果我这样做了,我会把它贴在这里,这样你们就可以看看,也许可以使用.
I will probably have to cook a function up. If I do so, I will post it here so you folks can look at it and maybe make use of.
推荐答案
您仅限于 小型函数列表,用于文本数据类型.
You are limited to small list of functions for text data type.
我只能建议从 PATINDEX
开始,但从 DATALENGTH-1、DATALENGTH-2、DATALENGTH-3
等开始向后工作,直到得到结果或结束在零(DATALENGTH-DATALENGTH)
All I can suggest is start with PATINDEX
, but work backwards from DATALENGTH-1, DATALENGTH-2, DATALENGTH-3
etc until you get a result or end up at zero (DATALENGTH-DATALENGTH)
这确实是 SQL Server 2000
根本无法处理的事情.
This really is something that SQL Server 2000
simply can't handle.
编辑其他答案:REVERSE 不在 SQL Server 2000 中可用于文本数据的函数列表中
Edit for other answers : REVERSE is not on the list of functions that can be used with text data in SQL Server 2000
这篇关于使用 T-SQL 查找子字符串最后一次出现的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!