Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

416
Views
Extract Substring in Comma Separated values in Postgresql

Two Variables in Postgresql:

V_VAR :='US185268001,US285268002,US385268005'; --Input
V_OUT := '001,002,005'; --Expected output to be stored

Any idea how to do in postgresql? Last 3 characters of each comma separated value. How to manipulate the V_VAR variable to get the output as V_OUT variable.

Update:

Please Note: V_VAR can have any number of CSV list. Here i have 3 values for the example purpose.

Thanks, Karthik

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

We can try using a regex replacement here:

SELECT
    col,
    REGEXP_REPLACE(col, '.*?([0-9]{3})(,|$)', '\1\2', 'g') AS col_out
FROM yourTable;

The strategy here is to match lazily until matching (and capturing) the final 3 digits of each code in one group, and an optional comma in a second group. Then, we replace with just the three digits and optional comma separator.

Note: As @BillKarvin has pointed out, storing unnormalized CSV is not optimal database design. Please consider getting each code onto a separate record if possible.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!