sql - Postgres : Select the maximum integer substring of a column value -
i'm using postgresql , i'm having trouble formulating right sql query. title of question might sound weird, i'm trying achieve. i'm performing data curation particular table. if have table product containing :
id | designation --------------------- 1 | ir15a1021 2 | ir15a1001 3 | ir15a1050 4 | ab100 5 | ar100 what want to:
1) integer substring of only , exactly records designation whose pattern starts 'ir15a'. , integer substring, meaning number after starting string 'ir15a'. (1021,1001,1050)
2) maximum integer substring in case :
maximum integer substring : 1050 because other ir15a have max substrings less (1021 , 1001).
assume correct conditions.
1) substring after ir15a integer no worrying type, integer. 2) ignore other designation patterns. ir15a.
i've managed '%ir15a%' wildcard search haven't found solution cut integer substring out of , compare others maximum. thanks!
try this:
select max(cast(substring(designation '....$') int)) tab designation 'ir15a%' - take rows
ir15ausinglike 'ir15a%' - remove
ir15ausingsubstring(designation '....$'). - convert int using
cast - take
maxvalue usingmax()
sql fiddle demo
Comments
Post a Comment