Alphanumeric number generation

  • Hi,

    I need to write a SQL query to print the following aphanumberic sequence in SQL 2008.

    0001, 0002, ... , 0009, 000A, ... , 000Z, ... , 0010, 0011, ... , 001A, ... and so on till... , ZZZZ

    all characters should be in UPPERCASE.

    Please Help me...

    Thanks in advance

  • Forgive me if I'm mistaken, but all of your posts look like homework. What have you tried so far?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Hi,

    not exactly like home work, as per my requirement only am posting.

    i've tried below code, but it is generating only number.

    WITH Sequence ( SeqNo) as

    (

    SELECT 1

    UNION ALL

    SELECT SeqNo + 1

    FROM Sequence

    WHERE SeqNo < 1000

    )

    SELECT TOP 1000 * FROM Sequence

    OPTION ( MAXRECURSION 0);

    GO

    please help me......

  • Use table constructor to generate your primary sequence, within a CTE. Then run a select from it, joined to itself 3 times (giving 4 references total in the FROM list). They can be CROSS JOINS or old fashioned comma joins. It works just fine. Have a try, post back if you are unsure.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Below code is working in 'Oracle'

    i need same output in sql.....

    with digits as

    ( select n, chr(mod(n,36)+case when mod(n,36) < 10 then 48 else 55 end) d

    from (Select rownum-1 as n from dual connect by level < 37)

    )

    select d0.n*36*36*36*36 + d1.n*36*36*36 + d2.n*36*36 + d3.n*36 + d4.n, d0.d||d1.d||d2.d||d3.d||d4.d

    from digits d0,digits d1, digits d2, digits d3, digits d4

    Plz

  • Skanda (10/8/2012)


    Hi,

    not exactly like home work, as per my requirement only am posting.

    i've tried below code, but it is generating only number.

    WITH Sequence ( SeqNo) as

    (

    SELECT 1

    UNION ALL

    SELECT SeqNo + 1

    FROM Sequence

    WHERE SeqNo < 1000

    )

    SELECT TOP 1000 * FROM Sequence

    OPTION ( MAXRECURSION 0);

    GO

    please help me......

    You're looking at the top 1000 in your query, have you thought about how many rows there will actually be?

    You have 36 characters in total for each digit (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z). So we're looking at 36*36*36*36 == 1,679,616 rows.


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Skanda (10/8/2012)


    Below code is working in 'Oracle'

    i need same output in sql.....

    with digits as

    ( select n, chr(mod(n,36)+case when mod(n,36) < 10 then 48 else 55 end) d

    from (Select rownum-1 as n from dual connect by level < 37)

    )

    select d0.n*36*36*36*36 + d1.n*36*36*36 + d2.n*36*36 + d3.n*36 + d4.n, d0.d||d1.d||d2.d||d3.d||d4.d

    from digits d0,digits d1, digits d2, digits d3, digits d4

    Plz

    Chris covered how in his post, did you try it?

    SELECT a.Chr+b.Chr+c.Chr+d.Chr

    FROM (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),

    ('7'),('8'),('9'),('A'),('B'),('C'),('D'),

    ('E'),('F'),('G'),('H'),('I'),('J'),('K'),

    ('L'),('M'),('N'),('O'),('P'),('Q'),('R'),

    ('S'),('T'),('U'),('V'),('W'),('X'),('Y'),

    ('Z')

    )a(Chr)

    CROSS JOIN (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),

    ('7'),('8'),('9'),('A'),('B'),('C'),('D'),

    ('E'),('F'),('G'),('H'),('I'),('J'),('K'),

    ('L'),('M'),('N'),('O'),('P'),('Q'),('R'),

    ('S'),('T'),('U'),('V'),('W'),('X'),('Y'),

    ('Z')

    )b(Chr)

    CROSS JOIN (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),

    ('7'),('8'),('9'),('A'),('B'),('C'),('D'),

    ('E'),('F'),('G'),('H'),('I'),('J'),('K'),

    ('L'),('M'),('N'),('O'),('P'),('Q'),('R'),

    ('S'),('T'),('U'),('V'),('W'),('X'),('Y'),

    ('Z')

    )c(Chr)

    CROSS JOIN (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),

    ('7'),('8'),('9'),('A'),('B'),('C'),('D'),

    ('E'),('F'),('G'),('H'),('I'),('J'),('K'),

    ('L'),('M'),('N'),('O'),('P'),('Q'),('R'),

    ('S'),('T'),('U'),('V'),('W'),('X'),('Y'),

    ('Z')

    )d(Chr)

    ORDER BY a.Chr, b.Chr, c.Chr, d.Chr;


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • in query trying for 1000,

    but i need max.

    if it is more than 36*36*36*36, it helps me a lot...

  • Thank U Boss,

    It is looking great...

    but i need every record should have a numeric and alpabet,

    in present code, some records are only alphabetic....

  • Skanda (10/8/2012)


    Hi,

    I need to write a SQL query to print the following aphanumberic sequence in SQL 2008.

    0001, 0002, ... , 0009, 000A, ... , 000Z, ... , 0010, 0011, ... , 001A, ... and so on till... , ZZZZ

    all characters should be in UPPERCASE.

    Please Help me...

    Thanks in advance

    Here's a TSQL equivalent of the Oracle CTE:

    SELECT

    n,

    CHAR(n + CASE WHEN n < 10 THEN 48 ELSE 55 END)

    FROM (SELECT TOP(36) n = ROW_NUMBER() OVER (ORDER BY (SELECT NULL))-1 FROM sys.columns a, sys.columns b) t

    ORDER BY n

    Can you figure it out from here?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Skanda (10/8/2012)


    Thank U Boss,

    It is looking great...

    but i need every record should have a numeric and alpabet,

    in present code, some records are only alphabetic....

    In your original post, you said: -

    Skanda (10/8/2012)


    0001, 0002, ... , 0009, 000A, ... , 000Z, ... , 0010, 0011, ... , 001A, ... and so on till... , ZZZZ

    "ZZZZ" doesn't have numeric values.


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Cadavre (10/8/2012)


    Skanda (10/8/2012)


    Thank U Boss,

    It is looking great...

    but i need every record should have a numeric and alpabet,

    in present code, some records are only alphabetic....

    In your original post, you said: -

    Skanda (10/8/2012)


    0001, 0002, ... , 0009, 000A, ... , 000Z, ... , 0010, 0011, ... , 001A, ... and so on till... , ZZZZ

    "ZZZZ" doesn't have numeric values.

    And "0001, 0002, ... , 0009..." have no alpha characters...

    I guess the method I've offered in the following thread might help you:

    http://www.sqlservercentral.com/Forums/Topic1267659-391-1.aspx

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • I wonder how many 3 and 4 letter swear words such a proccess will actually spell out.

    I strongly recommend not using incrementing alpha-numerics for just such a reason but if you do, you really need to avoid the use of vowels at the very least.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (10/27/2012)


    I wonder how many 3 and 4 letter swear words such a proccess will actually spell out...

    Actually it depends how you take it, at least some of them will be easy to remember :-D.

    By the way, English as a language is not very rich with such words in compare with some other languages (eg. world-champion in this business: Russian :hehe:)

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Eugene Elutin (10/29/2012)


    Jeff Moden (10/27/2012)


    I wonder how many 3 and 4 letter swear words such a proccess will actually spell out...

    Actually it depends how you take it, at least some of them will be easy to remember :-D.

    By the way, English as a language is not very rich with such words in compare with some other languages (eg. world-champion in this business: Russian :hehe:)

    That's why we shouldn't be Russian to use incrementing Alpha-Numerics. 😛

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 15 posts - 1 through 15 (of 26 total)

You must be logged in to reply to this topic. Login to reply