postgresql - How would you read a csv in a stored procedure such that the csv needs data extraction? -
the csv has urls of images in format -
www.domain.com/table_id/x_y_height_width.jpg
we want extract table_id, x, y, height , width these urls in stored procedure , use these parameters in multiple sql queries.
how can that?
regexp_split_to_array , split_part functions
create or replace function split_url ( _url text, out table_id int, out x int, out y int, out height int, out width int ) $$ select a[2]::int, split_part(a[3], '_', 1)::int, split_part(a[3], '_', 2)::int, split_part(a[3], '_', 3)::int, split_part(split_part(a[3], '_', 4), '.', 1)::int (values (regexp_split_to_array(_url, '/')) ) rsa(a); $$ language sql immutable; select * split_url('www.domain.com/234/34_12_400_300.jpg'); table_id | x | y | height | width ----------+----+----+--------+------- 234 | 34 | 12 | 400 | 300
to use function other tables lateral
:
with t (url) ( values ('www.domain.com/234/34_12_400_300.jpg'), ('www.examplo.com/984/12_90_250_360.jpg') ) select * t cross join lateral split_url(url) ; url | table_id | x | y | height | width ---------------------------------------+----------+----+----+--------+------- www.domain.com/234/34_12_400_300.jpg | 234 | 34 | 12 | 400 | 300 www.examplo.com/984/12_90_250_360.jpg | 984 | 12 | 90 | 250 | 360
Comments
Post a Comment