venki.msg (9/4/2012)
I need logic for generating following pattern.AA001A0 TO AA999A0 THIS IS FIRST SCENARIO.
NEXT IS AA001B0 TO AA999B0 IS SECOND SCENARIO.
If you have only these two fix scenario.Then you can try Jeff's Tally table as mentioned below.
--===== Do this in a nice safe place that everyone has
-- (You can build a permanent one in any database)
USE TempDB;
IF OBJECT_ID('TempDB..Tally','U') IS NOT NULL
DROP TABLE Tally;
GO
--===================================================================
-- Create a Tally table from 1 to 1000 (you can increase this number as per your requirement)
--===================================================================
--===== Create and populate the Tally table on the fly.
SELECT TOP 1000
IDENTITY(INT,1,1) AS N
INTO dbo.Tally
FROM Master.sys.ALL_Columns ac1
CROSS JOIN Master.sys.ALL_Columns ac2
;
--===== Add a CLUSTERED Primary Key to maximize performance
ALTER TABLE dbo.Tally
ADD CONSTRAINT PK_Tally_N
PRIMARY KEY CLUSTERED (N) WITH FILLFACTOR = 100
;
--===== Allow the general public to use it
GRANT SELECT ON dbo.Tally TO PUBLIC
;
GO
------ Now move to your query
SELECT 'AA' + RIGHT('00'+ CONVERT(VARCHAR,N),3) + 'A0' AS NUM
FROM tempdb.dbo.Tally
WHERE N < 1000
Likewise you can try out the second pattern on your own 🙂
--rhythmk
------------------------------------------------------------------
To post your question use below link
https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help
🙂