Skip to content

Instantly share code, notes, and snippets.

Created October 31, 2012 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3986663 to your computer and use it in GitHub Desktop.
Save anonymous/3986663 to your computer and use it in GitHub Desktop.
delimiter $$
drop function if exists counts_words_in_uppercase$$
create function counts_words_in_uppercase(
value_in varchar(500)
)
returns integer deterministic
begin
declare result_v integer default 0;
declare i integer default 1;
declare current_word_v varchar(255) default '';
declare char_v varchar(1);
declare is_in_upper_v tinyint;
while i <= length(value_in) do
set char_v = substr(value_in, i, 1);
if (char_v in ('.', ',', '.', '!', '?', ' ') or i = length(value_in)) and (length(current_word_v) > 0) then
if i = length(value_in) then
set current_word_v = concat(current_word_v, char_v);
end if;
select (current_word_v collate latin1_general_cs) REGEXP '^[A-Z]+$' into is_in_upper_v from dual;
set result_v = result_v + is_in_upper_v;
set current_word_v = '';
else
set current_word_v = concat(current_word_v, char_v);
end if;
set i = i + 1;
end while;
return result_v;
end$$
delimiter ;
-- here is a small test
select 'I find YOU And You aND YUo TOO' as `string to test`, counts_words_in_uppercase('I find YOU And You aND YUo TOO') as `cnt`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment