Split one column into multiple column

Steven Henry Suhendra 20 Reputation points
2024-11-08T02:30:55.4366667+00:00

Dear my friends,

I have a question about how to split one column into multiple column ?

I've try the code but it doesn't work

Screenshot 2024-11-08 092319 Does anyone could help me ?

Thank You for your help

Best Regards,

Steve Henry

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,213 questions
SQL Server Transact-SQL
SQL Server Transact-SQL
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Transact-SQL: A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
108 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,670 questions
0 comments No comments
{count} votes

Accepted answer
  1. LiHongMSFT-4306 29,516 Reputation points
    2024-11-08T02:53:58.76+00:00

    Hi @Steven Henry Suhendra

    Try this code:

    SELECT [No]
          ,[Code]
    	  ,CASE WHEN Code LIKE 'D%' THEN Departement END AS Departement
    	  ,CASE WHEN Code LIKE 'SD%' THEN Departement END AS Special_Departement
    FROM db.table;
    

    Best regards,

    Cosmog


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. PRADEEPCHEEKATLA 90,461 Reputation points
    2024-11-08T02:39:28.8933333+00:00

    @Steven Henry Suhendra - Thanks for the question and using MS Q&A platform.

    To split a single column into multiple columns based on certain conditions, you can use CASE statements to populate each column separately. Here’s an example based on your provided table.

    Your goal is to separate the "Department" column into "Department" and "Special Department" columns. You can use SQL like this:

    SELECT 
        No,
        Code,
        CASE 
            WHEN Code IN ('D001', 'D002') THEN Department 
            ELSE NULL 
        END AS Department,
        CASE 
            WHEN Code IN ('SD001', 'SD002') THEN Department 
            ELSE NULL 
        END AS Special_Department
    FROM 
        db.table;
    
    
    1. The first CASE statement checks if the Code is either 'D001' or 'D002' and assigns the value to the "Department" column. If the code doesn’t match, it returns NULL.
    2. The second CASE statement checks if the Code is either 'SD001' or 'SD002' and assigns the value to the "Special_Department" column. If the code doesn’t match, it returns NULL.

    This approach will give you the output you’re looking for, with "Department" values in one column and "Special Department" values in another.

    Hope this helps. Do let us know if you have any further queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.