String to Array and Array to String in PostgreSQL
📖 Scenario: You are managing a contact list database. Each contact has a list of phone numbers stored as a single string separated by commas. You want to convert this string into an array to work with individual phone numbers easily. Later, you want to convert the array back into a string to store or display.
🎯 Goal: Learn how to convert a comma-separated string into an array using PostgreSQL functions, and then convert an array back into a string.
📋 What You'll Learn
Create a table called
contacts with columns id (integer) and phone_numbers (text).Insert a row with
id = 1 and phone_numbers as a comma-separated string of phone numbers.Write a query to convert the
phone_numbers string into a text array.Write a query to convert the text array back into a comma-separated string.
💡 Why This Matters
🌍 Real World
Many databases store lists as strings. Converting between strings and arrays helps manipulate individual items easily.
💼 Career
Database developers and analysts often need to parse and format list data stored as strings for reporting and processing.
Progress0 / 4 steps