T-SQL LIKE 条件中的重复字符 [英] Repeating characters in T-SQL LIKE condition
本文介绍了T-SQL LIKE 条件中的重复字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题:
将 VARCHAR
变量(或列)的值限制为数字和 ASCI 字符,但允许可变长度.
此脚本不会产生所需的结果:
Problem:
Limit the value of a VARCHAR
variable (or a column) to digits and ASCI characters, but allow variable length.
This script will not yield required result:
declare @var as VARCHAR (150)
select @var = '123ABC'
if (@var LIKE '[a-zA-Z0-9]{0,150}')
print 'OK'
else
print 'Not OK'
有人知道怎么做吗?
推荐答案
您可以使用 not carat ^ 和 NOT LIKE 表达式执行此操作.
You can do this with the not carat ^, and a NOT LIKE expression.
所以你说,哪里不像非字母数字;) 这适用于标准数字 &字符:
So you say, where not like not non-alphanumeric ;) This works for standard numbers & characters:
declare @var as VARCHAR (150)
select @var = '123ABC'
if (@var NOT LIKE '%[^a-zA-Z0-9]%')
print 'OK'
else
print 'Not OK'
感谢 Martin 的整理提示,如果您希望将 ý 等字符视为非字母数字,请添加到 COLLATE 中,如下所示
Thanks Martin for the collation hint, if you want the characters like ý treated as non-alphanumeric add in the COLLATE as below
declare @var as VARCHAR (150)
select @var = '123ABCý'
if (@var NOT LIKE '%[^a-zA-Z0-9]%' COLLATE Latin1_General_BIN )
print 'OK'
else
print 'Not OK'
这篇关于T-SQL LIKE 条件中的重复字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文