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 8875143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:54:10+00:00 2026-06-14T18:54:10+00:00

I am having sql table in that I am having 2 fields as No

  • 0

I am having sql table in that I am having 2 fields as No and declaration

Code  Declaration
123   a1-2 nos, a2- 230 nos, a3 - 5nos

I need to display the declaration for that code as:

Code  Declaration 
123   a1 - 2nos 
123   a2 - 230nos 
123   a3 - 5nos

I need to split the column data to rows for that code.

  • 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-14T18:54:11+00:00Added an answer on June 14, 2026 at 6:54 pm

    For this type of data separation, I would suggest creating a split function:

    create FUNCTION [dbo].[Split](@String varchar(MAX), @Delimiter char(1))       
    returns @temptable TABLE (items varchar(MAX))       
    as       
    begin      
        declare @idx int       
        declare @slice varchar(8000)       
    
        select @idx = 1       
            if len(@String)<1 or @String is null  return       
    
        while @idx!= 0       
        begin       
            set @idx = charindex(@Delimiter,@String)       
            if @idx!=0       
                set @slice = left(@String,@idx - 1)       
            else       
                set @slice = @String       
    
            if(len(@slice)>0)  
                insert into @temptable(Items) values(@slice)       
    
            set @String = right(@String,len(@String) - @idx)       
            if len(@String) = 0 break       
        end   
    return 
    end;
    

    Then to use this in a query you can use an outer apply to join to your existing table:

    select t1.code, s.items declaration
    from yourtable t1
    outer apply dbo.split(t1.declaration, ',') s
    

    Which will produce the result:

    | CODE |  DECLARATION |
    -----------------------
    |  123 |     a1-2 nos |
    |  123 |  a2- 230 nos |
    |  123 |    a3 - 5nos |
    

    See SQL Fiddle with Demo

    Or you can implement a CTE version similar to this:

    ;with cte (code, DeclarationItem, Declaration) as
    (
      select Code,
        cast(left(Declaration, charindex(',',Declaration+',')-1) as varchar(50)) DeclarationItem,
             stuff(Declaration, 1, charindex(',',Declaration+','), '') Declaration
      from yourtable
      union all
      select code,
        cast(left(Declaration, charindex(',',Declaration+',')-1) as varchar(50)) DeclarationItem,
        stuff(Declaration, 1, charindex(',',Declaration+','), '') Declaration
      from cte
      where Declaration > ''
    ) 
    select code, DeclarationItem
    from cte
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I will be having a table in SQL Server 2008 that will hold millions
I have some asp.net code that populates the fields in a LINQ to SQL
I have a table in sql having two fields 'JOB_NUMBER' 'SRno', the relation between
I am having .sql (sybase) file which creates the table i.e. Create_table_mytable.sql But before
i m having following type of simple sql server table Here I want to
In SQL Server is it possible to perform table partition without having to recode
I'm having a problem in a SQL command. I have a table with questions,
I am having a few problems trying to return a table from a SQL
I'm having an sql error that i can't work out. I have to update
I am joining a table that has two record id fields (record1, record2) to

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.