Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a column value into two columns in a SELECT?

I have a string value in a varchar column. It is a string that has two parts. Splitting it before it hits the database is not an option.

The column's values look like this:

one_column:
'part1 part2'
'part1 part2'

So what I want is a a result set that looks like:

col1,col2:
part1,part2
part1,part2

How can I do this in a SELECT statement? I found a pgsql function to split the string into an array but I do not know how to get it into two columns.

like image 233
NJ. Avatar asked Sep 23 '11 05:09

NJ.


1 Answers

select split_part(one_column, ' ', 1) AS part1, 
       split_part(one_column, ' ', 2)  AS part2 ...
like image 88
Michael Lorton Avatar answered Nov 11 '22 02:11

Michael Lorton