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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:19:12+00:00 2026-05-25T14:19:12+00:00

Hi all i have the following script which is all fine and dandy and

  • 0

Hi all i have the following script which is all fine and dandy and does the job, however i don’t think it is very nice or efficient. Just wondering if there’s a better and more efficient way of doing the same? The job is to calculate the number of occurrences of numbers in a number string and then select the total. So for example with the number ‘014812000’, the expected outcome should be 6 as we have 4 x 0 occurrences and 2 x 1 occurrences. We are only concerned with occurrences over 1. Just to add this is being using in a user defined function.

DECLARE @Number nvarchar(50)
SET @Number = '014812000'

DECLARE @count0 int
DECLARE @count1 int
DECLARE @count2 int
DECLARE @count3 int
DECLARE @count4 int
DECLARE @count5 int
DECLARE @count6 int
DECLARE @count7 int
DECLARE @count8 int
DECLARE @count9 int

DECLARE @countTotal int
SET @countTotal = 0

SET @count0 = LEN(@Number) - LEN(REPLACE(@Number, '0', '')) 
SET @count1 = LEN(@Number) - LEN(REPLACE(@Number, '1', '')) 
SET @count2 = LEN(@Number) - LEN(REPLACE(@Number, '2', '')) 
SET @count3 = LEN(@Number) - LEN(REPLACE(@Number, '3', '')) 
SET @count4 = LEN(@Number) - LEN(REPLACE(@Number, '4', '')) 
SET @count5 = LEN(@Number) - LEN(REPLACE(@Number, '5', '')) 
SET @count6 = LEN(@Number) - LEN(REPLACE(@Number, '6', '')) 
SET @count7 = LEN(@Number) - LEN(REPLACE(@Number, '7', '')) 
SET @count8 = LEN(@Number) - LEN(REPLACE(@Number, '8', '')) 
SET @count9 = LEN(@Number) - LEN(REPLACE(@Number, '9', '')) 

IF @count0 > 1
    BEGIN
        SET @countTotal = @countTotal + @count0
    END
IF @count1 > 1
    BEGIN
        SET @countTotal = @countTotal + @count1
    END
IF @count2 > 1
    BEGIN
        SET @countTotal = @countTotal + @count2
    END
IF @count3 > 1
    BEGIN
        SET @countTotal = @countTotal + @count3
    END
IF @count4 > 1
    BEGIN
        SET @countTotal = @countTotal + @count4
    END
IF @count5 > 1
    BEGIN
        SET @countTotal = @countTotal + @count5
    END
IF @count6 > 1
    BEGIN
        SET @countTotal = @countTotal + @count6
    END
IF @count7 > 1
    BEGIN
        SET @countTotal = @countTotal + @count7
    END
IF @count8 > 1
    BEGIN
        SET @countTotal = @countTotal + @count8
    END
IF @count9 > 1
    BEGIN
        SET @countTotal = @countTotal + @count9
    END

SELECT @countTotal
  • 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-25T14:19:13+00:00Added an answer on May 25, 2026 at 2:19 pm

    You can use a number table to split the number to rows. Here I use master..spt_values.

    select sum(C)
    from (
           select count(*) as C
           from master..spt_values as N
           where N.type = 'P' and
                 N.number between 1 and len(@Number)
           group by substring(@Number, N.Number, 1)
           having count(*) > 1
         ) as T      
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi all i have the following merge sql script which works fine for a
I have the following script which runs fine but I think a) it is
I have a php script which is doing the following job. $unTarCommand = 'tar
Hi I have the following script which works fine running on IIS but when
I have the following script. It replaces all instances of @lookFor with @replaceWith in
The rails books and web pages I've been following have all stuck to very
I have the following in my code, which has worked fine in IE 7
I have the following code, which logs in and out of Facebook fine, but
I have developed the following user script which will show the html element under
I have the following script which slides a container down and another up, and

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.