Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Alphanumeric number generation


Alphanumeric number generation

Author
Message
Minnu
Minnu
Old Hand
Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)

Group: General Forum Members
Points: 305 Visits: 950
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9028 Visits: 19041
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
Exploring Recursive CTEs by Example Dwain Camps
Minnu
Minnu
Old Hand
Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)

Group: General Forum Members
Points: 305 Visits: 950
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......
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9028 Visits: 19041
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
Exploring Recursive CTEs by Example Dwain Camps
Minnu
Minnu
Old Hand
Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)

Group: General Forum Members
Points: 305 Visits: 950
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
Cadavre
Cadavre
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2598 Visits: 8437
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

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/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Cadavre
Cadavre
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2598 Visits: 8437
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

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/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Minnu
Minnu
Old Hand
Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)

Group: General Forum Members
Points: 305 Visits: 950
in query trying for 1000,
but i need max.

if it is more than 36*36*36*36, it helps me a lot...
Minnu
Minnu
Old Hand
Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)

Group: General Forum Members
Points: 305 Visits: 950
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....
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9028 Visits: 19041
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
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search