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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:23:28+00:00 2026-05-23T05:23:28+00:00

I have some data as below ID Data 1 a 1 2 1 b

  • 0

I have some data as below

ID Data
1   a
1   2
1   b
1   1
2   d
2   3
2   r

Desired Output

ID    Data
1     a
1     NULL
1     NULL
1     b
1     NULL
2     d
2     NULL
2     NULL
2     NULL
2     r

What the output is , for the numerics it is replaced with those many null values. E.g. for a numeric value of 2 , it will be 2 null values.

The ddl is as under

Declare @t table(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

Please give a CTE based solution. I already have the procedural approach but I need to have a feel of CTE based solution.

Solution I am using at present

CREATE FUNCTION [dbo].[SPLIT] (
 @str_in VARCHAR(8000)
)
RETURNS @strtable TABLE (id int identity(1,1), strval VARCHAR(8000))

AS

BEGIN

DECLARE @tmpStr VARCHAR(8000), @tmpChr VARCHAR(5), @ind INT = 1, @nullcnt INT = 0
SELECT @tmpStr = @str_in

WHILE LEN(@tmpStr) >= @ind
BEGIN

 SET @tmpChr = SUBSTRING(@tmpStr,@ind,1)
 IF ISNUMERIC(@tmpChr) = 0
  INSERT INTO @strtable SELECT @tmpChr
 ELSE
  WHILE @nullcnt < @tmpChr
  BEGIN
   INSERT INTO @strtable SELECT NULL
   SET @nullcnt = @nullcnt + 1
  END
 SELECT @ind = @ind + 1, @nullcnt = 0

END

RETURN

END
GO

Invocation

SELECT * from dbo.SPLIT('abc2e3g')

I donot want to do it using function but a CTE solution.

Reason: I am learning CTE and trying to solve problems using it. Trying to come out of procedural/rBar approach.

Thanks

  • 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-23T05:23:28+00:00Added an answer on May 23, 2026 at 5:23 am

    Setup:

    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'
    

    Query:

    ;with cte
    as
    (
        select UniqueId, id, data, case when isnumeric(data) = 1 then cast(data as int) end Level
        from @t
        union all
        select UniqueId, id, null, Level - 1
        from cte
        where Level > 0
    )
    select id, data
    from cte
    where Level is null or data is null
    order by UniqueID, Level
    

    Output:

    id          data
    ----------- ----------
    1           a
    1           NULL
    1           NULL
    1           b
    1           NULL
    2           d
    2           NULL
    2           NULL
    2           NULL
    2           r
    

    I added UniqueId as there should be some kind of field that specifies order in the original table.

    • 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 some data on a single line like below abc edf xyz rfg
I need to have some data members get some values when I create an
My db structure is as below and I also have some data. How do
I have some data that is constrained below a 1:1 line. I would to
I have wrote a class (a snippet is below) which have some data members
I have some data with messed-up accented characters. For example in the data we
I have some data grouped in a table by a certain criteria, and for
I have some data of the form Key ID Link 1 MASTER 123 2
I have some data. I want to go through that data and change cells

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.