re: Find longest word in a given string VIEW POST

VIEW FULL DISCUSSION
 
CREATE OR REPLACE FUNCTION get_longest_word (val TEXT) RETURNS TEXT AS $$
DECLARE longest_word TEXT;
BEGIN
  SELECT strs INTO longest_word
  FROM regexp_split_to_table(val, '\s+') AS strs
  ORDER BY char_length(strs) DESC
  LIMIT 1;

  RETURN longest_word;
END;
$$ LANGUAGE plpgsql;

SELECT get_longest_word('a bc def ghij klm no p');
 
code of conduct - report abuse