Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a single column values to multiple column values?

I have a problem splitting single column values to multiple column values.

For Example:

Name ------------ abcd efgh ijk lmn opq asd j. asdjja asb (asdfas) asd asd 

and I need the output something like this:

first_name             last_name ---------------------------------- abcd                     efgh ijk                      opq asd                      asdjja asb                      asd asd                      null 

The middle name can be omitted (no need for a middle name) The columns are already created and need to insert the data from that single Name column.

like image 707
Shahsra Avatar asked Feb 25 '11 22:02

Shahsra


People also ask

How do I split a single row into multiple columns?

In the table, click the cell that you want to split. Click the Layout tab. In the Merge group, click Split Cells. In the Split Cells dialog, select the number of columns and rows that you want and then click OK.

How do you split columns based on cell value?

Select the cell, range, or entire column that contains the text values that you want to split. On the Data tab, in the Data Tools group, click Text to Columns. Follow the instructions in the Convert Text to Columns Wizard to specify how you want to divide the text into separate columns.


1 Answers

Your approach won't deal with lot of names correctly but...

SELECT CASE          WHEN name LIKE '% %' THEN LEFT(name, Charindex(' ', name) - 1)          ELSE name        END,        CASE          WHEN name LIKE '% %' THEN RIGHT(name, Charindex(' ', Reverse(name)) - 1)        END FROM   YourTable  
like image 173
Martin Smith Avatar answered Oct 01 '22 06:10

Martin Smith