有没有办法在SQL LIKE中使用模式匹配,以匹配具有上限的可变数量的字符?
例如,我有一列可以有2-10个数字的“正确值”,任何大于10小于2的值都是不正确的。
发布于 2017-01-16 23:54:16
如果你真的想使用like,你可以这样做:
where col like '__' or col_like '___' or . . .或者:
where col like '__%' and -- at least two characters
col not like '_________%' -- not 9 characters更典型的方法是:
where len(col) between 2 and 10如果您想确保它们是数字:
where len(col) between 2 and 10 and
col not like '%[^0-9]%'发布于 2017-01-16 23:29:43
您可以创建一个函数来检查每个字符,如下所示:
DECLARE @TempField varchar(max)= '1v3Abc567'
DECLARE @FieldLength int = LEN(@TempField)
DECLARE @CountNumeric int = 0
WHILE @FieldLength > 0
BEGIN
SELECT @CountNumeric = @CountNumeric + ISNUMERIC( SUBSTRING (@TempField,@FieldLength,1))
SET @FieldLength = @FieldLength - 1
END
SELECT @CountNumeric NumericCount发布于 2017-01-16 23:22:56
您可以使用Regex。
SELECT * FROM mytable WHERE mycol LIKE '%[0-9]{2,10}%';应该这么做(这是我想不到的,所以请仔细检查!
https://stackoverflow.com/questions/41679571
复制相似问题