[Solved] Dividing one column into two columns in SQL [duplicate]


try this:

DECLARE @YourTable table (Column1  varchar(50))
INSERT @YourTable VALUES ('Frodo Baggins')
INSERT @YourTable VALUES ('Samwise Gamgee')
INSERT @YourTable VALUES ('Peregrin Took')
INSERT @YourTable VALUES ('Meriadoc Brandybuck')
INSERT @YourTable VALUES ('aa')
INSERT @YourTable VALUES ('aa bb cc')

SELECT
    LEFT(Column1,CHARINDEX(' ',Column1)) AS Names
    ,RIGHT(Column1,LEN(Column1)-CHARINDEX(' ',Column1)) AS Surnames
    FROM @YourTable

--both queries produce same output
SELECT 
    SUBSTRING(Column1, 0, CHARINDEX(' ', Column1))
    ,SUBSTRING(Column1, CHARINDEX(' ',Column1) + 1, LEN(Column1)) 
FROM @YourTable

OUTPUT:

Names       Surnames
----------- -------------
Frodo       Baggins
Samwise     Gamgee
Peregrin    Took
Meriadoc    Brandybuck
            aa
aa          bb cc

(6 row(s) affected)

1

solved Dividing one column into two columns in SQL [duplicate]