August 6th, 2012, 08:37 AM
Pl/pgsql function to count number of same words in two strings
hello forum! could please anyone help me to write a pl/pgsql function to count number of same words in two given strings?
I've given up here:
CREATE OR REPLACE FUNCTION word_match (
1ststr text, 2ndstr text
) RETURNS integer AS $$
a=string_2_array(1ststr, ' ')
FOR n<array_upper(a) LOOP
a[n] ilike '%||b||%'
Than my friend told me that probably it is better to do it through regex like this:
select count(*) from regexp_split_to_table( 'string one', ' ') a where a in (select regexp_split_to_table( 'string two', ' '));
but just for the matter of interest could anyone tell how to do it through pl/pgsql?
thanks a lot! :)))
August 8th, 2012, 11:06 AM
Your friend's solution seems perfectly valid and simple. Just throw it into a plpgsql function...
August 8th, 2012, 11:37 AM
yeah thanks! in the end i've just wrote pl/pgsql function based on his suggestion...just wondered probably there is some another way, that doesn't use regex functions....