Tuesday, July 9, 2013

Find out the Upper caracter count in a string

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

/*Output*/

select

dbo.CountUpperCase('SQLserver2008')

output

=3




No comments:

Post a Comment