简体   繁体   English

有没有办法在T-SQL中获取指定字符的第N个索引? (没有子串!)

[英]Is there a way to get the Nth index of a specified char in T-SQL? (Without Substring!)

I am trying to get the index of the Nth character inside a varchar(MAX). 我试图获取varchar(MAX)中第N个字符的索引。 Right now I am cutting off the front end of the varchar so I only have to check for the first but With extremely large varchars the process is getting way too slow. 现在,我切断了varchar的前端,因此只需要检查第一个即可,但是对于非常大的varchars,该过程变得太慢了。 I would like to parse through the varchar(max) and just keep track of what index I am at and get the next character I want AFTER that index. 我想通过varchar(max)进行解析,只是跟踪我所在的索引,并在该索引之后获取我想要的下一个字符。 If I could do this without having to constantly cut off the front of a large varchar constantly I think it would increase performance a good amount. 如果我可以做到这一点而不必不断地切断大型varchar的前端,我认为它将大大提高性能。 Thanks. 谢谢。

EDIT: Right now to parse a large amount of text I use CHARINDEX to get the index of the char, THEN I SUBSTRING the text to remove up to that first index. 编辑:现在要解析大量文本,我使用CHARINDEX来获取char的索引,然后我对文本进行SUBSTRING删除直到第一个索引。 Now I call CHARINDEX again (which effectively retrieves the 2nd index of that character inside the text). 现在,我再次调用CHARINDEX(它有效地检索了文本内该字符的第二个索引)。 However, this SUBSTRINGING is very taxing on the system and I want to avoid it. 但是,这种SUBSTRINGING会对系统造成很大的负担,我想避免这种情况。

EDIT: Ah, sorry my title was very misleading, it should be more straight forward now. 编辑:啊,对不起,我的头衔很容易让人误解,现在应该更直接了。

The built-in string functions are rather limited in T-SQL. 内置的字符串函数在T-SQL中相当有限。 You need to combine SUBSTRING(), CHARINDEX(), STRPOS() to do this, but it not fast operations. 您需要结合使用SUBSTRING(),CHARINDEX(),STRPOS()来执行此操作,但这不是快速的操作。

Also you can use a tally table. 您也可以使用理货单。

But i think the best way is to use CLR procedure for task like your. 但是我认为最好的方法是对您这样的任务使用CLR程序。 It will be more efficient and fast. 它将更加高效和快捷。

One way to increase performance when doing procedural work (as opposed to set-based work) is to use CLR Integration with SQL Server . 在执行过程工作(与基于集合的工作相反)时,提高性能的一种方法是将CLR与SQL Server集成在一起 I got this tip from Chris Lively in this answer when asking a question about performance. 当我问性能问题时,我从克里斯·利弗利(Chris Lively)那里得到了这个提示。 Although I haven't tested it myself, the documentation around CLR integration with SQL server suggests that the CLR is much more performant than T-SQL for procedural operations. 尽管我自己还没有测试过,但是有关CLR与SQL Server集成的文档表明,对于程序操作,CLR的性能比T-SQL高得多。 For set-based operations, though, use T-SQL. 但是,对于基于集合的操作,请使用T-SQL。

我认为CHARINDEXSUBSTRING函数将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM