简体   繁体   English

如何在T-SQL中查找第一个出现的一个字符序列

[英]How to find first occurrence of one of a sequence of characters in T-SQL

I would like to write a simple query to find the first occurrence of either ',' or ';' 我想写一个简单的查询来查找第一次出现','或';' in a string within T-SQL. 在T-SQL中的字符串中。 Is there any easy way to do this? 有没有简单的方法来做到这一点?

In other databases I would use a regular expression such as [,;] but these aren't available in T-SQL. 在其他数据库中,我会使用正则表达式,如[,;]但这些在T-SQL中不可用。

The only solution I can think of is to have a long list of nested if .. else statements but that doesn't appeal. 我能想到的唯一解决方案是拥有一长串嵌套的if ... else语句,但这并不吸引人。

Try PATINDEX... 试试PATINDEX ......

select patindex('%,%', my_column)
from my_table

I am vastly ignorant of T-SQL, but looking at Microsoft's documentation it seems like CHARINDEX will find the first occurrence of a single character (or in fact of any string), and you can just call it twice (once for , and once for ; ) and see which one occurs first. 我对T-SQL一无所知,但是看看微软的文档,似乎CHARINDEX会发现第一次出现单个字符(或者实际上是任何字符串),你可以调用它两次(一次用于,一次用于; )并查看哪一个首先出现。 See: http://msdn.microsoft.com/en-US/library/ms186323%28v=sql.90%29.aspx . 请参阅: http//msdn.microsoft.com/en-US/library/ms186323%28v=sql.90%29.aspx

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

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