Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7775321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:36:05+00:00 2026-06-01T17:36:05+00:00

In T-SQL, I need to separate a TEXT value by new lines character and

  • 0

In T-SQL, I need to separate a TEXT value by new lines character and then loop on each line.

In other words, I need an explode() function, but for T-SQL, and I need to to loop on each line.

I tough about creating a new temporary table and storing lines of the string in this table as rows, and then loop on this with a cursor. I’m just not sure about how to separate a string.

Is there a quick way to do this in T-SQL ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:36:06+00:00Added an answer on June 1, 2026 at 5:36 pm

    (I supposed your data would be larger than 4000 caracters.)

    The split function:

    CREATE FUNCTION dbo.Split
    (
        @RowData nvarchar(2000),
        @SplitOn nvarchar(5)
    )  
    RETURNS @RtnValue table 
    (
        Id int identity(1,1),
        Data nvarchar(2000)
    ) 
    AS  
    BEGIN 
        Declare @Cnt int
        Set @Cnt = 1
    
        While (Charindex(@SplitOn,@RowData)>0)
        Begin
            Insert Into @RtnValue (data)
            Select 
                Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
    
            Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
            Set @Cnt = @Cnt + 1
        End
    
        Insert Into @RtnValue (data)
        Select Data = ltrim(rtrim(@RowData))
    
        Return
    END
    

    And the explode function:

    CREATE FUNCTION dbo.*tableName*_Explode
    (
        @id int
    ) 
    RETURNS @RtnValue table 
    (
        Id int,
        Data NVARCHAR(2000)
    )
    AS
    BEGIN
        DECLARE @LINEBREAK AS varchar(2)
        SET @LINEBREAK = CHAR(13) + CHAR(10)
    
        DECLARE @short_text NVARCHAR(2000)
        DECLARE @short_text_length INT
    
        DECLARE @sub_length INT
        SET @sub_length = 2000
        DECLARE @sub_index INT
        SET @sub_index = 1
    
    
    BEGIN
        SET @short_text = (select SUBSTRING(*tableData*,@sub_index,@sub_length) from *tableName* WHERE id = @id)
        SET @short_text_length = DATALENGTH(@short_text)
    
                WHILE (@short_text_length > 0)
        BEGIN
            Insert Into @RtnValue (id,data)
            SELECT @id, @short_text
    
            SET @sub_index = (@sub_index + @sub_length)
            SET @short_text = (select SUBSTRING(*tableData*,@sub_index,@sub_length) from *tableName* WHERE id = @id)
            SET @short_text_length = DATALENGTH(@short_text)
        END
    
        IF (SELECT COUNT(*) FROM @RtnValue) = 0
            Insert Into @RtnValue (id,data)
            SELECT @id, ''
        END
    
        RETURN
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 150+ SQL queries in separate text files that I need to analyze
I'm somewhat new to SQL and need help with query syntax. My issue involves
I am very new to SQL and i need to retreive a number of
I need to retrieve data from MsAccess/SQL Server (two separate projects in which I
I need to calculate subtotals in a separate line, but I cannot use ROLLUP
I have comma-separated values. I need a query to insert into SQL Server 2008
I have a pretty simple SQL I need to perform. I have a ProcessUser
I need a SQL query to make all data in a column UPPER CASE?
I need an sql query to give me the following: All the values in
i need an sql script that is going to change the datasource of all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.