Postgresql varchar to int -
i have clothes size column in postgresql containing values such (24, 25, "s", "m", "onesize", "xl").
i need extract integer values , convert result int. tried to_number in format below, works if row contains number, not work if contains letters.
select to_number('aq1', '99999d9') -> 1 select to_number('23', '99999d9') -> 23 select to_number('onesize', '99999d9') -> error
i need function work in way allow me join on varchar column equal int column.
select ???func("xl") -> null/0 select ???func(23) -> 23
any appreciated
try one:
select coalesce((select unnest(regexp_matches('xlds', '\d+')::integer[])), 0);
notice regexp matches first number in string.
Comments
Post a Comment