我知道,以下查询返回行,其中包含A
和G
之间的确切5个字符
select *
from
(select 'prefixABBBBBGsuffix' code /*this will be returned. */
union
select 'prefixABBBBGsuffix') rex
where
code like '%A_____G%'
但是我想在A
和G
之间有17个角色,那么like
条件必须有17个下划线。所以我在谷歌搜索我发现[]
会用在像。然后我试着这样做。
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).*/
然后它返回NULL集。如何在集合[]
中搜索具有一定数量字符的行?
注意:
我正在使用SQL Server 2012。
与以前相同的答案,但纠正。 17不是数字,它是18和19的字符串,也放在len(text betweenA和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
我找不到任何有关处理这类字符的方法的工作信息,复制只是一种简化参数化和输入的方法。
我会用REPLICATE
生成所需数量的'_':
select * from (
select 'prefixABBBBBGsuffix' code
union
select 'prefixABBBBGsuffix'
) rex
where code like '%A' + REPLICATE('_',17) + 'G%';