sql - how to extract specific word from string in Postgres -


product name contains words deliminated space. first word size second in brand etc.

how extract words string, e.q how implement query like:

select   id,          getwordnum( prodname,1 ) size,   getwordnum( prodname,2 ) brand   products ({0} null or getwordnum( prodname,1 )={0} ) ,     ({1} null or getwordnum( prodname,2 )={1} )   create table product ( id char(20) primary key, prodname char(100) ); 

how create getwordnum() function in postgres or should substring() or other function used directly in query improve speed ?

you try use function split_part

select   id,          split_part( prodname, ' ' , 1 ) size,   split_part( prodname, ' ', 2 ) brand   products ({0} null or split_part( prodname, ' ' , 1 )= {0} ) ,     ({1} null or split_part( prodname, ' ', 2 )= {1} ) 

Comments