SQL 服务器中的通配符表达式

Wildcard expression in SQL Server

我知道,以下查询 returns 行,这些行包含 AG

之间的确切 5 个字符
select * 
from 
     (select 'prefixABBBBBGsuffix' code /*this will be returned. */
      union 
      select 'prefixABBBBGsuffix') rex 
where 
     code like '%A_____G%' 

但是我想要AG之间的17个字符,那么like条件必须有17个下划线。所以我搜索了一下 google 我发现 [] 会用在 like 中。然后我试了一下。

select * 
from 
    (select 'AprefixABBBBBGsuffixG' code 
     union 
     select 'AprefixABBBBGsuffixG') rex 
where 
    code like '%A[_]^17G%' /*As per my understanding, '[]' makes a set. And  
                               '^17' would be power of the set (like Mathematics).*/

然后它returns NULL 集。如何在集合 []?

中搜索具有一定字符数的行

注:

我正在使用 SQL Server 2012。

我会使用 REPLICATE 生成所需数量的“_”:

select * from (
  select 'prefixABBBBBGsuffix' code 
  union 
  select 'prefixABBBBGsuffix'  
) rex 
where code like '%A' + REPLICATE('_',17) + 'G%';

与之前相同的答案,但已更正。 17不是数字,字符串是18和19,也放在len(textbetweenA and G)中显示。

select rex.* 
from (
    select len('prefixABBBBBGsuffix') leng, 'AprefixABBBBBGsuffixG' code 
    union 
    select len('prefixABBBBGsuffix'), 'AprefixABBBBGsuffixG'
    union
    select 0, 'A___________________G'
    ) rex 
where 
    rex.code like '%A' + replicate('_',19) + 'G%'

--and with [] the set would be [A-Za-z]. Notice this set does not match the A___________________G string.

select rex.* 
from (
    select len('prefixABBBBBGsuffix') leng, 'AprefixABBBBBGsuffixG' code 
    union 
    select len('prefixABBBBGsuffix'), 'AprefixABBBBGsuffixG'
    union
    select 0, 'A___________________G'
    ) rex 
where 
    rex.code like '%A' + replicate('[A-Za-z]',19) + 'G%'

[A-Za-z0-9] 匹配字母范围内的 一个 字符(两种情况)或数字 0 到 9

我找不到关于另一种处理大量字符的方法的任何工作信息,复制只是一种简化参数化和键入的方法。