Splitting rows into multiple rows based on column values where delimitter is space

alfygraham 31 Reputation points
2021-09-08T08:58:36.023+00:00

I need to split the rows of table into multiple rows where delimitter is space,currently this is sql server 2012, so STRING_SPLIT is not working and compatibility is 110.In the given table task_No is auto incremented.

130149-image.png

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,150 questions
SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
2,950 questions
SQL Server Analysis Services
SQL Server Analysis Services
A Microsoft online analytical data engine used in decision support and business analytics, providing the analytical data for business reports and client applications such as Power BI, Excel, Reporting Services reports, and other data visualization tools.
1,297 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,665 questions
{count} vote

Accepted answer
  1. EchoLiu-MSFT 14,591 Reputation points
    2021-09-09T07:36:18.21+00:00

    Hi @alfygraham ,

    Welcome to the microsoft TSQL Q&A forum!

    Since you did not provide the expected output, I did a test with my own data:

    CREATE TABLE SourcetableB(ID INT,String VARCHAR(MAX))  
    INSERT INTO SourcetableB VALUES(1,'abc def pqr xyz')  
    ,(2,'pqr xyz ghi abc')  
                                 
    --Create user-defined functions(applies to SQL Server all supported versions)   
    CREATE FUNCTION SplitStr(@Sourcestr VARCHAR(8000), @Seprate VARCHAR(100))       
    RETURNS @result TABLE(F1 VARCHAR(100))       
      AS         
       BEGIN       
       DECLARE @sql AS VARCHAR(100)       
      SET @Sourcestr=@Sourcestr+@Seprate         
      WHILE(@Sourcestr<>'')       
      BEGIN       
        SET @sql=left(@Sourcestr,CHARINDEX(' ',@Sourcestr,1)-1)       
        INSERT @result VALUES(@sql)       
         SET @Sourcestr=STUFF(@Sourcestr,1,CHARINDEX(' ',@Sourcestr,1),'')       
       END       
       RETURN    
       END  
    GO  
      
    SELECT *   
    FROM SourcetableB s   
    CROSS APPLY SplitStr(S.string,' ') V;  
      
    DROP FUNCTION SplitStr  
    DROP TABLE SourcetableB  
    

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 113.6K Reputation points MVP
    2021-09-08T21:23:28.01+00:00

    This short article on my web site includes a string-splitter for low-level versions, and has examples how split columns in tables: https://www.sommarskog.se/arrays-in-sql.html.

    You really need to review the design of that table. For each column should ask yourself what it means that a column is NULL. If you don't know the answer, the column should be NOT NULL. (For instance, what does a row with an ID and all NULL mean?)

    You also need to review the data types. Columns by the name of Start_Date and Close_Date should probably be of the data type date. using string for date and time will cause you grief. Also, bear in mind that MAX columns comes with overhead, so don't use it when you don't need it.

    1 person found this answer helpful.
    0 comments No comments

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.