如何在字符串中检查大写的存在长度 - Sql Query

时间:2021-10-02 09:15:06

How to check upper case existence length in a string using Sql Query?

如何使用Sql Query检查字符串中的大写存在长度?

For Eg:

对于Eg:

1.KKart - from this string the result should be 2, because it has 2 upper case letters. 2.WPOaaa - from this string the result should be 3, because it has 3 upper case letters.

1.KKart - 从这个字符串开始,结果应为2,因为它有2个大写字母。 2.WPOaaa - 从这个字符串开始,结果应为3,因为它有3个大写字母。

Thanks in advance

提前致谢

2 个解决方案

#1


4  

There is no built-in T-SQL function for that.
You can use a user-defined function like this one:

没有内置的T-SQL功能。您可以使用这样的用户定义函数:

CREATE FUNCTION CountUpperCase
(
    @input nvarchar(50)
)
RETURNS int
AS
BEGIN

    declare @len int
    declare @i int
    declare @count int
    declare @ascii int

    set @len = len(@input)
    set @i = 1
    set @count = 0

    while @i <= @len
    begin

        set @ascii = ascii(substring(@input, @i, 1))

        if @ascii >= 65 and @ascii <= 90
        begin
            set @count = @count +1
        end

        set @i = @i + 1

    end

    return @count

END

Usage (with the examples from your question):

用法(使用您问题中的示例):

select dbo.CountUpperCase('KKart') returns 2.
select dbo.CountUpperCase('WPOaaa') returns 3.

select dbo.CountUpperCase('KKart')返回2. select dbo.CountUpperCase('WPOaaa')返回3。

#2


-1  

How about something like this :

这样的事情怎么样:

SELECT len(replace(my_string_field,'abcdefghijklmnopqrstuvwxyz','')) as 'UpperLen'
FROM my_table

The principle is simply to replace all lower case char by nothing and counts the remaining.

原则是简单地将所有小写字母替换为空白并计算其余字符。

#1


4  

There is no built-in T-SQL function for that.
You can use a user-defined function like this one:

没有内置的T-SQL功能。您可以使用这样的用户定义函数:

CREATE FUNCTION CountUpperCase
(
    @input nvarchar(50)
)
RETURNS int
AS
BEGIN

    declare @len int
    declare @i int
    declare @count int
    declare @ascii int

    set @len = len(@input)
    set @i = 1
    set @count = 0

    while @i <= @len
    begin

        set @ascii = ascii(substring(@input, @i, 1))

        if @ascii >= 65 and @ascii <= 90
        begin
            set @count = @count +1
        end

        set @i = @i + 1

    end

    return @count

END

Usage (with the examples from your question):

用法(使用您问题中的示例):

select dbo.CountUpperCase('KKart') returns 2.
select dbo.CountUpperCase('WPOaaa') returns 3.

select dbo.CountUpperCase('KKart')返回2. select dbo.CountUpperCase('WPOaaa')返回3。

#2


-1  

How about something like this :

这样的事情怎么样:

SELECT len(replace(my_string_field,'abcdefghijklmnopqrstuvwxyz','')) as 'UpperLen'
FROM my_table

The principle is simply to replace all lower case char by nothing and counts the remaining.

原则是简单地将所有小写字母替换为空白并计算其余字符。