UPDATE tbl_cdr SET real_dst = substring(dstchannel FROM '\/([^\/\-]+)\-') WHERE dst LIKE 'queue%' AND substring(dst FROM 6) ~ '^[0-9]+$' AND substring(dstchannel FROM '\/([^\/\-]+)\-') IS NOT NULL; UPDATE tbl_cdr SET dst = CONCAT('queue-',(SELECT name FROM tbl_queues WHERE id = CAST(regexp_replace(tbl_cdr.dst, '\D*(\d+)', '\1') AS bigint) LIMIT 1)) WHERE dst LIKE 'queue%' AND substring(dst FROM 6) ~ '^[0-9]+$'; 23