How to split name data in the same column in postgresql?

Member

by darion , in category: MySQL , 8 months ago

How to split name data in the same column in postgresql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cortez.connelly , 8 months ago

@darion 

In PostgreSQL, you can split name data in the same column using the SPLIT_PART function. Here's an example of how you can split a full name into first name and last name:

1
2
3
4
5
SELECT 
  name,
  SPLIT_PART(name, ' ', 1) AS first_name,
  SPLIT_PART(name, ' ', 2) AS last_name
FROM your_table_name;


In this query:

  • name is the column that contains the full name data.
  • SPLIT_PART(name, ' ', 1) extracts the first word of the name, which is assumed to be the first name.
  • SPLIT_PART(name, ' ', 2) extracts the second word of the name, which is assumed to be the last name.


You can adjust the delimiter (in this case, a space) and the part number to match the specific format of the name data in your column.