﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 7,2000 / T-SQL  / Increment in sql server. / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 20 Jun 2013 03:38:37 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Increment in sql server.</title><link>http://www.sqlservercentral.com/Forums/Topic1353709-8-1.aspx</link><description>Perhaps something like this:[code="sql"]DECLARE @t TABLE (ID INT IDENTITY, strcol VARCHAR(20))INSERT INTO @tSELECT 'aa001' UNION ALL SELECT 'ab001'UNION ALL SELECT 'aa001a0' UNION ALL SELECT 'aa001b0';WITH Tally (n) AS (    SELECT TOP 999 RIGHT('00' + CAST(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS VARCHAR), 3)    FROM sys.all_columns)SELECT STUFF(strcol, m, 3, n) FROM @tCROSS APPLY (SELECT CHARINDEX('001', strcol)) a(m)CROSS APPLY TallyORDER BY ID, n[/code]</description><pubDate>Thu, 06 Sep 2012 23:50:10 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Increment in sql server.</title><link>http://www.sqlservercentral.com/Forums/Topic1353709-8-1.aspx</link><description>Thank but only one value inserting in a table it's not incremented.example in table only aa001 only displaying it should be incremented till aa999 na.</description><pubDate>Wed, 05 Sep 2012 00:05:36 GMT</pubDate><dc:creator>venki.msg</dc:creator></item><item><title>RE: Increment in sql server.</title><link>http://www.sqlservercentral.com/Forums/Topic1353709-8-1.aspx</link><description>if you can have an identity column in your table, you can create a calculated persisted column that auto generates that texty-like value.[code]--#################################################################################################--Pattern: AA000 to ZZ999 max value=676000--#################################################################################################IF OBJECT_ID('X') IS NOT NULL   DROP TABLE XCREATE TABLE X(XID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CHECK ((XID&amp;gt; 0) AND (XID &amp;lt; 676000)), --limit=26 * 26 + 999 + 1XCALCULATED AS   CHAR((XID/26000)%26+65) --1st Letter       +CHAR((XID/1000)%26+65)  --2nd Letter       +REPLACE(STR(XID%1000,3),' ','0')  PERSISTED, --The 3 digit numeric partSOMEOTHERCOL VARCHAR(30))INSERT INTO X(SOMEOTHERCOL) VALUES('WHATEVER')SET IDENTITY_INSERT X ON INSERT INTO X(XID,SOMEOTHERCOL) VALUES(675999,'MORESTUFF')SET IDENTITY_INSERT X OFFSET IDENTITY_INSERT X ON INSERT INTO X(XID,SOMEOTHERCOL) VALUES(676000,'MORESTUFF') --FAILS! too big!SET IDENTITY_INSERT X OFFSELECT * FROM X--three char table: bigger range--#################################################################################################--Pattern: AAA000 to ZZZ999 max value=196040000--#################################################################################################IF OBJECT_ID('X') IS NOT NULL   DROP TABLE XCREATE TABLE X(XID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CHECK ((XID&amp;gt; 0) AND (XID &amp;lt; 196040000)),XCALCULATED AS      CHAR((XID/260000)%26+65) --1st Letter   + CHAR((XID/26000)%26+65)  --2nd Letter  + CHAR((XID/1000)%26+65)   --3rd Letter  + REPLACE(STR(XID%10000,4),' ','0') PERSISTED, --The 4 digit numeric partSOMEOTHERCOL VARCHAR(30))INSERT INTO X(SOMEOTHERCOL) VALUES('WHATEVER')SET IDENTITY_INSERT X ON INSERT INTO X(XID,SOMEOTHERCOL) VALUES(675999,'MORESTUFF')SET IDENTITY_INSERT X OFFSET IDENTITY_INSERT X ON INSERT INTO X(XID,SOMEOTHERCOL) VALUES(676000,'MORESTUFF')SET IDENTITY_INSERT X OFFSELECT * FROM XXID XCALCULATED SOMEOTHERCOL 1 AA001 WHATEVER 675999 ZZ999 MORESTUFF [/code]</description><pubDate>Tue, 04 Sep 2012 06:52:22 GMT</pubDate><dc:creator>Lowell</dc:creator></item><item><title>RE: Increment in sql server.</title><link>http://www.sqlservercentral.com/Forums/Topic1353709-8-1.aspx</link><description>thank you:-)</description><pubDate>Tue, 04 Sep 2012 06:47:47 GMT</pubDate><dc:creator>venki.msg</dc:creator></item><item><title>RE: Increment in sql server.</title><link>http://www.sqlservercentral.com/Forums/Topic1353709-8-1.aspx</link><description>[quote][b]venki.msg (9/4/2012)[/b][hr]I need logic for generating following pattern.AA001A0 TO AA999A0 THIS IS FIRST SCENARIO.NEXT IS AA001B0 TO AA999B0 IS SECOND SCENARIO.[/quote]If you have only these two fix scenario.Then you can try Jeff's Tally table as mentioned below.[code="sql"]--===== 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.TallyWHERE N &amp;lt; 1000[/code]Likewise you can try out the second pattern on your own :-)</description><pubDate>Tue, 04 Sep 2012 04:42:54 GMT</pubDate><dc:creator>rhythmk</dc:creator></item><item><title>RE: Increment in sql server.</title><link>http://www.sqlservercentral.com/Forums/Topic1353709-8-1.aspx</link><description>I need logic for generating following pattern.AA001A0 TO AA999A0 THIS IS FIRST SCENARIO.NEXT IS AA001B0 TO AA999B0 IS SECOND SCENARIO.</description><pubDate>Tue, 04 Sep 2012 03:34:33 GMT</pubDate><dc:creator>venki.msg</dc:creator></item><item><title>RE: Increment in sql server.</title><link>http://www.sqlservercentral.com/Forums/Topic1353709-8-1.aspx</link><description>[quote][b]venki.msg (9/3/2012)[/b][hr]Hi all i need to do increment a number like aa001 to aa999 then again ab001 to ab999aa001a0 to aa999a0 then aa001b0 to aa999b0.Can any one help me out to find logic for this.[/quote]Sorry I am confused with your examples [quote]aa001 to aa999 then again ab001 to ab999aa001a0 to aa999a0 then aa001b0 to aa999b0[/quote]Could you please explain your exact requirement with clear expected output.</description><pubDate>Tue, 04 Sep 2012 00:10:14 GMT</pubDate><dc:creator>rhythmk</dc:creator></item><item><title>Increment in sql server.</title><link>http://www.sqlservercentral.com/Forums/Topic1353709-8-1.aspx</link><description>Hi all i need to do increment a number like aa001 to aa999 then again ab001 to ab999aa001a0 to aa999a0 then aa001b0 to aa999b0.Can any one help me out to find logic for this.</description><pubDate>Mon, 03 Sep 2012 23:23:51 GMT</pubDate><dc:creator>venki.msg</dc:creator></item></channel></rss>