如何检查文本是数字还是大写? [英] how do I check if a text is digit or uppercase?
问题描述
大家好!
我需要检查数字或大写或混合的东西。
我不能允许像!,<,#
例子
1)'aA1' - >没有
2)'A1' - > ok
3)'1' - > ok
4)'1a' - >没有
我试过常规表达式:
Hi everyone!
I need something that check if a digit or uppercase or mixed.
I can't allow character like !,<,#
example
1) 'aA1'-->no
2) 'A1' -->ok
3) '1' -->ok
4) '1a' -->no
I have tried with regular expression :
DECLARE @PROVA AS VARCHAR(50)=''
SELECT CASE WHEN @PROVA LIKE '%[A-Z]%' Collate Latin1_General_CS_AI
THEN 'ok' ELSE 'NO'
END
但它并不适用于所有情况。
可以你帮我吗?
谢谢你
but it doesn't work in every case.
Can you help me?
thank you
推荐答案
我这样做了
I have done in this way
DECLARE @position int
declare @string char(15)
declare @error int,@no_error int
set @error =0
SET @position = 1
SET @string = '123A5'
IF LEN(@string)=5
BEGIN
WHILE @position <= LEN(@string)
BEGIN
if ASCII(SUBSTRING(@string, @position, 1)) between 65 and 90 or ASCII(SUBSTRING(@string, @position, 1)) between 48 and 57
set @no_error=1
else
set @error=2
SET @position = @position + 1
END
END
ELSE
set @error=2
select @error
这只会检查是否有一个大写的字符。
如果你想检查整个字符串是否为大写,那么将它与自己进行比较大写:
That will only check if there is a single char that is uppercase.
If you want to check if the whole string is uppercase then compare it to itself in uppercase:
SELECT
CASE WHEN @PROVA = UPPER(@PROVA)
THEN 'ok'
ELSE 'NO'
END
更新:OP需要限制特殊字符。
有几个选项依赖根据您的需求:
1:解析器功能
这可能是最简单的解决方案。创建一个检查字符串的每个字符的函数。它很容易实现和维护,但是如果你在足够大的桌子上进行操作,它可能会很慢。
2:正则表达式:
实施起来有点棘手但右手非常强大。请看一下这篇文章:
https://www.simple-talk.com/sql/t-sql-programming/tsql-regular-expression-workbench/ [ ^ ]
希望有所帮助^ _ ^
Andy
UPDATE: OP needs to restrict special chars.
There are a couple of options depending on your needs:
1: Parser function
This is probably the simplest solution. Create a function that checks each char of the string. It's easy to implement and maintain, but it can be very slow if you perform the operation on a large enough table
2: Regular Expressions:
A little trickier to implement but very powerful in the right hands. Take a look at this article for more:
https://www.simple-talk.com/sql/t-sql-programming/tsql-regular-expression-workbench/[^]
Hope that helps ^_^
Andy
这篇关于如何检查文本是数字还是大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!