How to check the length of existence of uppercase in a string - Sql Query

How to check the length of existence of an uppercase in a string using Sql Query?

For instance:

1.KKart - from this line, the result should be equal to 2, because it has 2 uppercase letters. 2.WPOaaa - the result should be 3 from this line, because it has 3 uppercase letters.

Thank you in advance

+4
source share
2 answers

There is no built-in T-SQL function for this.
You can use a custom function like this:

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 examples from your question):

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

+4
source

How about something like this:

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

The principle is to simply replace all lower case char with nothing and count the rest.

-1
source

All Articles