Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Alphanumeric number generation Expand / Collapse
Author
Message
Posted Monday, October 8, 2012 5:38 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 26, 2014 7:36 AM
Points: 229, Visits: 730
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
Post #1369729
Posted Monday, October 8, 2012 5:45 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:49 AM
Points: 6,828, Visits: 14,070
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
Post #1369734
Posted Monday, October 8, 2012 5:49 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 26, 2014 7:36 AM
Points: 229, Visits: 730
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......
Post #1369735
Posted Monday, October 8, 2012 5:59 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:49 AM
Points: 6,828, Visits: 14,070
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
Post #1369743
Posted Monday, October 8, 2012 6:02 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 26, 2014 7:36 AM
Points: 229, Visits: 730
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
Post #1369746
Posted Monday, October 8, 2012 6:05 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Saturday, October 25, 2014 11:42 AM
Points: 2,380, Visits: 7,591
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.



Not a DBA, just 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


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1369747
Posted Monday, October 8, 2012 6:09 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Saturday, October 25, 2014 11:42 AM
Points: 2,380, Visits: 7,591
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;




Not a DBA, just 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


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1369753
Posted Monday, October 8, 2012 6:11 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 26, 2014 7:36 AM
Points: 229, Visits: 730
in query trying for 1000,
but i need max.

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


Post #1369757
Posted Monday, October 8, 2012 6:16 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 26, 2014 7:36 AM
Points: 229, Visits: 730
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....
Post #1369761
Posted Monday, October 8, 2012 6:20 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:49 AM
Points: 6,828, Visits: 14,070
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
Post #1369764
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse