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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:05:47+00:00 2026-06-03T03:05:47+00:00

In SQL SERVER 2008 R2, I need to get a count of items contained

  • 0

In SQL SERVER 2008 R2, I need to get a count of items contained in a string that can have any of the following characteristics (controlled by the user and not the system):

  • each item separated by a comma
  • sequential items summarized by the first and last item separated by a dash
  • the non-incremental character attached to only the first item in a dash separated range
  • multiple characters representing the non-incremental portion of the designation
  • combination of the above

All of the following are possible:

  • R1,R2,R3,R4
  • R1-R4
  • R1-4
  • CP10-CP12
  • R1,R15-R19,RN5

If they were all comma separated, I could just count the commas +1, but that is actually less common than the other options.

A method to count the last option should work for all options. The result should be 7

My expected approach would be to:

  1. separate the items without a dash but separated by a comma => get a count
  2. isolate the dash separated items and remove the non-incremental character(s)
  3. subtract the smaller number from the larger number and add 1
  4. Add that number to the first number for a total count

I am totally stuck on even where to begin. Any ideas?

  • 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-03T03:05:48+00:00Added an answer on June 3, 2026 at 3:05 am

    This can be cleaned up/optimized and is intentionally verbose but should get you started. Notably, the logic inside the last IF is almost identical to that of the WHILE and the block that gets the numeric value of the left/right elements is repeated four times.

    declare @input varchar(max)
    set @input = 'R1,R15-R19,RN5-RN6'
    
    select @input
    
    declare @elements table
    (
        Element varchar(10),
        [Count] int
    )
    
    declare @element varchar(10)
    declare @index int
    declare @count int
    declare @left varchar(10)
    declare @right varchar(10)
    declare @position int
    
    while (len(@input) > 0 and charindex(',', @input) > 0)
    begin
        set @element = substring(@input, 0, charindex(',', @input))
        if (charindex('-', @element) > 0)
        begin
            set @index = charindex('-', @element)
            set @left = left(@element, @index - 1)
            set @right = substring(@element, @index + 1, len(@element) - len(@left))
    
            set @position = 0
            while (isnumeric(substring(@left, @position, 1)) = 0)
            begin
                set @position = @position + 1
            end
            set @left = substring(@left, @position, len(@left))
    
            set @position = 0
            while (isnumeric(substring(@right, @position, 1)) = 0)
            begin
                set @position = @position + 1
            end
            set @right = substring(@right, @position, len(@right))
    
            set @count = cast(@right as int) - cast(@left as int) + 1
        end
        else
        begin
            set @count = 1
        end
        insert into @elements select @element, @count
        set @input = replace(@input, @element + ',', '')
    end
    
    if (len(@input) > 0)
    begin
        set @element = @input
        if (charindex('-', @element) > 0)
        begin
            set @index = charindex('-', @element)
            set @left = left(@element, @index - 1)
            set @right = substring(@element, @index + 1, len(@element) - len(@left))
    
            set @position = 0
            while (isnumeric(substring(@left, @position, 1)) = 0)
            begin
                set @position = @position + 1
            end
            set @left = substring(@left, @position, len(@left))
    
            set @position = 0
            while (isnumeric(substring(@right, @position, 1)) = 0)
            begin
                set @position = @position + 1
            end
            set @right = substring(@right, @position, len(@right))
    
            set @count = cast(@right as int) - cast(@left as int) + 1
        end
        else
        begin
            set @count = 1
        end
        insert into @elements select @element, @count
    end
    
    select * from @elements
    select sum([Count]) from @elements
    

    Outputs the following results:

    R1,R15-R19,RN5-RN6
    
    R1      1
    R15-R19 5
    RN5-RN6 2
    
    8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Microsoft SQL server 2008 I have a query that is in need of
I have a SQL Server 2008 database which contains data that I need to
I need to localize a SQL Server 2008 database. After investigating recommendations, I have
I need a valid SQL Server 2008 connection string for Entity Framework 4.1 code-first
I have SQL Server 2008 and a JPA (EclipseLink) app. I need to be
In MS SQL Server 2008. How do I count the number of rows that
I need to query Sql Server and get an ID that identifies the machine
I have created a complex sql server 2008/coldfusion search page, that searches thru a
I have a process that is inserting data into a database (SQL Server 2008)
I need to import the following XML into SQL Server 2008 R2: <Role ID=ef63f979-c65d-421a-a214-7319c6279e13

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.