0

In TSQL, how to check a string include "a" followed by "b", and "b" not followed by "c"? For example, these strings is what I want:

ab
cab
acb

This is not what I want: abc

Note that you can insert any number of characters between the characters in the example(but not break my rule described above).

4

5 回答 5

6

使用 SQL 通配符%定义字符串中的任何字符(一个或多个),然后将其与LIKE运算符组合:

WHERE txtcol LIKE '%a%b%' AND txtcol NOT LIKE '%b%c%'
于 2012-05-24T11:36:57.623 回答
0

我会这样做没有类似的:

where charindex('a', string) < charindex('b', string) and
      charindex('c', string) < charindex('b', string)

如果你需要角色在那里,那么包括:

where charindex('a', string) < charindex('b', string) and
      charindex('c', string) < charindex('b', string) and
      charindex('a', string) > 0
于 2012-05-24T14:18:22.873 回答
-1
col1 like '%ab%' and col1 not like '%abc%'
于 2012-05-24T11:35:11.193 回答
-2

尝试这个:

WHERE string LIKE '%ab%' AND string NOT Like '%bc%'
于 2012-05-24T11:35:01.387 回答
-3

尝试这个:

字符字母 = 'c';

if (字母 == 'z') nextChar = 'a'; 否则 if (letter == 'Z') nextChar = 'A';

否则 nextChar = (char)((((整数)字母) + 1);

于 2012-05-24T12:58:32.713 回答