This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Check the below input data and expected output to divide a column data into multiple columns in SQL Server.
Input data:
1 2 3 4 5 |
col1 -------------------------- part1 part2 part3 abc xyz pqr one two three |
Expected Output:
1 2 3 4 5 |
C1 C2 C3 ---------- ---------- ---------- part1 part2 part3 abc xyz pqr one two three |
Create a table with sample data:
1 2 3 4 5 6 |
CREATE TABLE tbl_SplitColumns (col1 varchar(max)) GO INSERT INTO tbl_SplitColumns VALUES ('part1 part2 part3'),('abc xyz pqr'),('one two three') GO |
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
DECLARE @delimiter VARCHAR(5) SET @delimiter=' ' ;WITH CTE AS ( SELECT col1, CAST('' + REPLACE(col1, @delimiter , '') + '' AS XML) AS XMLString FROM tbl_SplitColumns ) SELECT XMLString.value('/A[1]', 'varchar(10)') As C1 ,XMLString.value('/A[2]', 'varchar(10)') As C2 ,XMLString.value('/A[3]', 'varchar(10)') As C3 FROM CTE GO |
Please try the different solution for this puzzle and share it via comment...