[英]Test for Upper Case - T-Sql
All,全部,
How can I check if a specified varchar character or entire string is upper case in T-Sql?如何检查指定的 varchar 字符或整个字符串在 T-Sql 中是否为大写? Ideally I'd like to write a function to test if a character is upper case, then I can later apply that to a generic varchar.
理想情况下,我想编写一个 function 来测试字符是否为大写,然后我可以稍后将其应用于通用 varchar。 It should return false for non alphabetic characters.
对于非字母字符,它应该返回 false。 I am only interested in english language characters.
我只对英文字符感兴趣。
I am working with T-sql in SQL Management Studio, and I have tried pulling records beginning with a lower case letter from a table in this fashion:我在 SQL Management Studio 中使用 T-sql,我尝试以这种方式从表中提取以小写字母开头的记录:
select * from TABLE
where SUBSTRING(author,1,1) != LOWER(SUBSTRING(author,1,1))
Which returns 0 records, but I know there are records beginning with upper and lower case letters.它返回 0 条记录,但我知道有以大写和小写字母开头的记录。
Thanks谢谢
EDIT: Since both podiluska and joachim-isaksoon have successfully answered my question (Both methods work for my purposes), would someone mind explaining which would be the most efficient method to use to query a table with a large number of records to filter out records with authors beginning with or without a capital letter?
编辑:由于podiluska和joachim-isaksoon都成功地回答了我的问题(这两种方法都适合我的目的),有人会介意解释一下哪种方法最有效地用于查询具有大量记录的表以过滤掉记录作者以大写字母开头还是不以大写字母开头?
Using collations使用排序规则
eg:例如:
if ('a'='A' Collate Latin1_General_CI_AI)
print'same 1'
else
print 'different 1'
if ('a'='A' Collate Latin1_General_CS_AI)
print'same 2'
else
print 'different 2'
The CS in the collation name indicates Case Sensitive (and CI, Case Insensitive).排序规则名称中的 CS 表示区分大小写(和 CI,不区分大小写)。 The AI/AS relates to accent sensitivity.
AI/AS 与口音敏感度有关。
or in your example或在您的示例中
SUBSTRING(author,1,1) <> LOWER(SUBSTRING(author,1,1)) COLLATE Latin1_General_CS_AI
To check if ch is upper case, and is a character that can be converted between upper and lower case (ie excluding non alphabetic characters);检查ch是否为大写,是否为可大小写转换的字符(即不包括非字母字符);
WHERE UNICODE(ch) <> UNICODE(LOWER(ch))
something like就像是
declare @v varchar(10) = 'ABC', @ret int = 0
select @ret = 1 where upper(@v)=@v COLLATE SQL_Latin1_General_CP1_CS_AS
select @ret
Lower case letters have the same ASCII or UNICODE value as their upper-case version with the exception that lower case letters have a bit flag of 32 set.小写字母具有与其大写版本相同的 ASCII 或 UNICODE 值,但小写字母的位标志设置为 32。
This is very easy to detect directly, or to wrap the detection into User-defined functions like IsUpper() and IsLower().这很容易直接检测,或者将检测包装到用户定义的函数中,如 IsUpper() 和 IsLower()。
Example (note that both ASCII() and UNICODE() work interchangeably here):示例(注意 ASCII() 和 UNICODE() 在这里可以互换使用):
DECLARE @Letter char(1);
SET @Letter = 'A'
PRINT
@Letter + space(1) +
CASE
WHEN (UNICODE(@Letter) & 32 > 0) THEN 'is lower case'
ELSE 'is UPPER CASE'
END
SET @Letter = 'z'
PRINT
@Letter + space(1) +
CASE
WHEN (ASCII(@Letter) & 32 > 0) THEN 'is lower case'
ELSE 'is UPPER CASE'
END
-- Output:
-- A is UPPER CASE
-- z is lower case
This function will look for any upper case letters in an entire string using a recursive CTE:此函数将使用递归 CTE 查找整个字符串中的任何大写字母:
CREATE FUNCTION [dbo].[ContainsUpper](@InputString [varchar](80))
RETURNS [bit] WITH EXECUTE AS CALLER
AS
BEGIN
DECLARE @Result bit = 0,
@LowerString varchar(80) = lower(@InputString);
WITH Letters
AS
(
SELECT Position = 1,
IsUpper =
CASE unicode(left(@InputString,1))
WHEN unicode(left(@LowerString,1)) THEN 0
ELSE 1
END
UNION ALL
SELECT Position = Position + 1,
IsUpper =
CASE unicode(substring(@InputString, Position + 1, 1))
WHEN unicode(substring(@LowerString, Position + 1, 1)) THEN 0
ELSE 1
END
FROM Letters
WHERE Position < len(@InputString)
)
SELECT @Result = max(convert(int, IsUpper))
FROM Letters
RETURN @Result
END
3 easy steps to check string is uppercase or lowercase program in PL/SQL using both在 PL/SQL 中使用两者检查字符串是大写还是小写的 3 个简单步骤
1.Function
2.procedure
using function使用 function
step:1 there are two variable步骤:1 有两个变量
step:2 one is to stored your string and the other is to convert your string into uppercase步骤:2 一个是存储您的字符串,另一个是将您的字符串转换为大写
step:3 the check condition if variable_one != variable_convert then it show string is lowercase else show string is uppercase步骤:3 检查条件 if variable_one != variable_convert 那么它显示字符串是小写 否则显示字符串是大写
set serveroutput on
declare
string_one varchar2(5);
string_convert varchar2(5);
begin
string_one:=&string;
string_convert:=UPPER(string_one);
if string_one != string_convert then
dbms_output.put_line('string is lowercse');
else
dbms_output.put_line('string is UPPERCASE');
end if;
end;
/
using procedure使用程序
check string is an uppercase or lowercase program in pl/sql using procedure logic is the same but no need to declare a variable change
check string是大写还是小写程序在pl/sql中使用procedure逻辑是一样的但是不需要声明变量变化
set serveroutput on
create or replace procedure upper_cse(str_one varchar2)
is
begin
if str_one != UPPER(str_one) then
dbms_output.put_line('string is lowercse');
else
dbms_output.put_line('string is UPPERCASE');
end if;
end;
/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.