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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:04:34+00:00 2026-05-23T07:04:34+00:00

I have the below data UniqueID ID data 1 1 a 2 1 2

  • 0

I have the below data

UniqueID    ID  data
1       1   a
2       1   2
3       1   b
4       1   1
5       2   d
6       2   3
7       2   r

The expected output being

ID  ConcatData
1   a,-,-,b,-
2   d,-,-,-,r

What we have to do is that, the number of numeric charecters has to be replaced with those many dashes(‘-‘) and then we need to merge the data for the respective id’s.

I am using the below query so far

declare @t table(UniqueID int identity(1,1), ID int, data varchar(10))  
insert into @t select 1, 'a' union all  select 1, '2' union all select 1, 'b' 
union all select 1, '1' union all select 2, 'd' union all select 2, '3' 
union all select 2, 'r' 

select * from @t

;with cte1 as 
(     
    select 
        UniqueId
        , id
        , data
        , case when isnumeric(data) = 1 then cast(data as int) end Level
         from @t     
    union all     
    select 
        UniqueId
        , id
        , CAST('-' as varchar(10))
        , Level - 1     
    from cte1     
    where Level > 0 ) 
,cte2 as
(
select id, GroupID = Dense_Rank() Over(Order by id),data, DataOrder = ROW_NUMBER() over(order by UniqueID, Level)
from cte1 
where Level is null or data = '-' 
) 

SELECT
ID
, (select data + ',' 
from cte2 t2 
where t2.GroupID = t1.GroupID
for XML path('')
) as ConcatData
from cte2 t1
group by t1.ID ,t1.GroupID

But the output is

ID  ConcatData
1   a,b,-,-,-,
2   d,r,-,-,-,

That is I am not able to position the dashes(‘-‘) in between the characters.

Please help

  • 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-05-23T07:04:34+00:00Added an answer on May 23, 2026 at 7:04 am

    Try this:

    ;with cte1 as
    (
    select 
    UniqueId
    , id
    , data
    ,case when isnumeric(data) = 1 
        THEN replicate(',-',data) 
        ELSE ',' + data end as string
    from @t
    )
    
    select 
    id
    ,LTRIM(STUFF(
        (
        SELECT
          ' ' + t2.String
        FROM Cte1 t2
        WHERE t2.id = t1.id
        FOR XML PATH('')
        ), 2, 1, ''
      )) As concatenated_string
    from cte1 t1 group by t1.ID ,t1.ID 
    

    Works for the sample data bove and might be a bit quicker than using cursors

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the below markup that cannot be changed: <div>Some data</div> <span>More data</span> <a>Link
I have a data that looks like this . And my code below simply
I have a table that contains the below data I want to write a
I have data like below AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE Now there is a
I have three tables tag , page , pagetag With the data below page
I have the code below : public class Anything { public int Data {
I have data stored as below in an MS Access database: Date User 20090101
I have a data structure which is as given below: class File { public
I have written this query for retrieving data from mysql as below select FeedbackCode,EMailID,FeedbackDetail,
I have a python script which outputs lots of data, sample is as below.

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.