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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:19:00+00:00 2026-06-07T21:19:00+00:00

I have written the script below to generate all possible permutations of a specified

  • 0

I have written the script below to generate all possible permutations of a specified set of characters.

Is there a way to efficiently do this while allowing the length to be specified at runtime?

DECLARE @string NVARCHAR(MAX)
SELECT @string = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

DECLARE @Chars TABLE (
    C CHAR(1) PRIMARY KEY
)

DECLARE @N INT 
SET @N = 1 
WHILE @N <= LEN(@string) BEGIN
    INSERT @Chars (
        C
    ) VALUES (
        SUBSTRING(@string, @N, 1)
    )

    SET @N = @N + 1
END

--SELECT * FROM @Chars


SELECT
    A.C + B.C + C.C
FROM @Chars A, @Chars B, @Chars C
ORDER BY
    A.C,
    B.C,
    C.C
  • 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-07T21:19:02+00:00Added an answer on June 7, 2026 at 9:19 pm

    After receiving comments related to the number of possible permutations, I realized that there should be very tight limits on the length of the character set as well as the specified length.

    Given that the allowable length would be extremely small, I see no harm in using a condition in my script… like this:

    DECLARE
        @string NVARCHAR(40), --limit the length of the character set to 40
        @length INT -- the limit for this is in a validation check below
    
    SELECT
        @string = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
        @length = 2
    
    
    
    IF (@length < 1 OR @length > 4) BEGIN
        RAISERROR('Invalid length specified. This function only supports lengths from 1 to 4', 16, 1)
    END
    
    
    
    DECLARE @Chars TABLE (
        C CHAR(1) PRIMARY KEY
    )
    
    ;WITH numbers AS (
        SELECT TOP(LEN(@string)) number
        FROM master..spt_values
        WHERE type = 'P'
        AND number != 0
    )
    
    INSERT @Chars (
        C
    )
    SELECT
        C = SUBSTRING(@string, number, 1)
    FROM numbers
    
    
    IF (@length = 1) BEGIN
        SELECT C FROM @Chars ORDER BY C
    END ELSE IF (@length = 2) BEGIN
        SELECT A.C + B.C
        FROM @Chars A, @Chars B
        ORDER BY A.C, B.C
    END ELSE IF (@length = 3) BEGIN
        SELECT A.C + B.C + C.C
        FROM @Chars A, @Chars B, @Chars C
        ORDER BY A.C, B.C, C.C
    END ELSE IF (@length = 4) BEGIN
        SELECT A.C + B.C + C.C + D.C
        FROM @Chars A, @Chars B, @Chars C, @Chars D
        ORDER BY A.C, B.C, C.C, D.C
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a Python script that will generate a series of graphs and
I have written a Perl script for the following bioinformatics question, but unfortunately there
I have written this random password script which works perfectly. Although I would like
I have written an SQL script having the below query. The query works fine.
I have little problem with printing data like this, I have written script like
I have written a PHP script below try to Post the JSON encoded string
I have written the script run.sh below for calling a java class : java
Below is the script I have written to change the value of one of
I have written the following script below - I am not quite happy with
All, Below is the code that I have written in JS. <html> <head> <title>Fibonacci

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.