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

Member

by darion , in category: MySQL , a year ago

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cortez.connelly , a year 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.

Related Threads:

How to use dynamic column name in postgresql?
How to query nested jsonb format data column in postgresql?
How to split string in postgresql?
How to filter table with timestamp column in postgresql?
How to split two columns array into rows in postgresql?
How to split city, state, country string using postgresql?