Sql Servers Charindex Function in Postgresql

I originally had a need for a charindex like function in postgresql sometime last week, and after some discussion on irc PJMODOS was kind enough to give me this: CREATE OR REPLACE FUNCTION CHARINDEX(text, text, integer) RETURNS integer AS $$ SELECT CASE WHEN strpos(substr($2, $3+1), $1) = 0 THEN 0 ELSE strpos(substr($2, $3+1), $1) + $3 END; $$ LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT; Since I needed it again today and had to spend a bunch of time looking for it, I figured I ought to blog it so I could find it in the future,so here it is. reference links for charindex: [http://lejalgenes.com/techtips/tips/Microsoft_SQL_Server/Find_second_occurrence_on_string_in_MSSQL.php http://lejalgenes.com/techtips/tips/Microsoft_SQL_Server/Find_second_occurrence_on_string_in_MSSQL.php] [http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.blocks/html/blocks/blocks120.htm http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.blocks/html/blocks/blocks120.htm]