Auto Increment Alpha Numeric Field in SQL

时间:2016-04-04 17:45:16

标签: sql sql-server ssms increment alpha

I was wondering if there is an easy way to do this, instead of my reinventing the wheel here. I am using SSMS. There is a number JKTUTS0 that needs to auto increment. So the next one would be JKTUTS1, JKTUTS2, JKTUTS3... then JKTUTT0

This is what I have so far, but it seems overly complicated and I'm stuck at this point. (It is not the primary key.) Is there an easier way to do this, or can someone please help me out? I'm still new to all this. Thanks.

DECLARE @IssueRecid CHAR(15) = (SELECT MAX(recid) FROM InventoryIssues) 
DECLARE @IssueNumber CHAR(10) = (SELECT IssueNumber FROM InventoryIssues WHERE recid=@IssueRecid)  
-- Returns JKTUTS0  

DECLARE @IssueNumber1 CHAR(1) = (SELECT LEFT (@IssueNumber, 1))  
DECLARE @IssueNumber2 CHAR(1) = (SELECT SUBSTRING (@IssueNumber, 2,1)) 
DECLARE @IssueNumber3 CHAR(1) = (SELECT SUBSTRING(@IssueNumber, 3,1))  
DECLARE @IssueNumber4 CHAR(1) = (SELECT SUBSTRING(@IssueNumber, 4,1)) 
DECLARE @IssueNumber5 CHAR(1) = (SELECT SUBSTRING(@IssueNumber, 5, 1))
DECLARE @IssueNumber6 CHAR(1) = (SELECT SUBSTRING(@IssueNumber, 6, 1)) 
DECLARE @IssueNumber7 CHAR(1) = (SELECT SUBSTRING(@IssueNumber, 7, 1))

IF @IssueNumber7 = 9 
BEGIN 
DECLARE @NewIssueNumber7 CHAR(1) = 0
DECLARE @NewIssueNumber6 CHAR(1) = needs to be the letter after S

2 个答案:

答案 0 :(得分:0)

I'm sure there are several ways to do this, all of which are greatly simplified by having access to the db to create functions and such. Assuming you have no such access, here some SQL that will increment the issue number as you have requested:

declare @IssueNumber char(7) = 'JKTUTS1'

declare @c1 char(1) = substring(@IssueNumber, 1, 1)
       ,@c2 char(1) = substring(@IssueNumber, 2, 1)
       ,@c3 char(1) = substring(@IssueNumber, 3, 1)
       ,@c4 char(1) = substring(@IssueNumber, 4, 1)
       ,@c5 char(1) = substring(@IssueNumber, 5, 1)
       ,@c6 char(1) = substring(@IssueNumber, 6, 1)
       ,@c7 char(1) = substring(@IssueNumber, 7, 1)
       ,@rtn varchar(8) = null

if @c7 <> '9'
begin
  set @c7 = char(ascii(@c7) + 1)
end
else if @c6 <> 'Z'
begin
  set @c7 = '0'
  set @c6 = char(ascii(@c6) + 1)
end
else if @c5 <> 'Z'
begin
  set @c7 = '0'
  set @c6 = 'A'
  set @c5 = char(ascii(@c5) + 1)
end
else if @c4 <> 'Z'
begin
  set @c7 = '0'
  set @c6 = 'A'
  set @c5 = 'A'
  set @c4 = char(ascii(@c4) + 1)
end
else if @c3 <> 'Z'
begin
  set @c7 = '0'
  set @c6 = 'A'
  set @c5 = 'A'
  set @c4 = 'A'
  set @c3 = char(ascii(@c3) + 1)
end
else if @c2 <> 'Z'
begin
  set @c7 = '0'
  set @c6 = 'A'
  set @c5 = 'A'
  set @c4 = 'A'
  set @c3 = 'A'
  set @c2 = char(ascii(@c2) + 1)
end
else if @c1 <> 'Z'
begin
  set @c7 = '0'
  set @c6 = 'A'
  set @c5 = 'A'
  set @c4 = 'A'
  set @c3 = 'A'
  set @c2 = 'A'
  set @c1 = char(ascii(@c1) + 1)
end
else
  set @rtn = 'OVERFLOW'

set @rtn = coalesce(@rtn, @c1 + @c2 + @c3 + @c4 + @c5 + @c6 + @c7)

select @rtn as NewIssueNumber

答案 1 :(得分:0)

这是一个稍微更通用的解决方案:

DECLARE @PreviousIssueNumber char(7),
        @NewIssueNumber char(10);

SET @PreviousIssueNumber = 'JKTUTT9';

DECLARE @LastCharacter char(1);
SET @LastCharacter = RIGHT(@PreviousIssueNumber, 1);

IF @LastCharacter != '9'
BEGIN
    SET @LastCharacter = CHAR(ASCII(@LastCharacter) + 1);
    SET @NewIssueNumber = LEFT(@PreviousIssueNumber, 6) + @LastCharacter;
END
ELSE
BEGIN
    SET @LastCharacter = '0';

    DECLARE @StringIndex int;
    SET @StringIndex = 6;

    DECLARE @CharacterAtStringIndex CHAR;
    SET @CharacterAtStringIndex = SUBSTRING(@PreviousIssueNumber, @StringIndex, 1);

    WHILE @CharacterAtStringIndex = 'Z'
    BEGIN
        SET @StringIndex = @StringIndex - 1;
        SET @CharacterAtStringIndex = SUBSTRING(@PreviousIssueNumber, @StringIndex, 1);
    END

    SET @CharacterAtStringIndex = CHAR(ASCII(@CharacterAtStringIndex) + 1);
    SET @NewIssueNumber = LEFT(STUFF(@PreviousIssueNumber, @StringIndex, 6 - @StringIndex, 'AAAAAA'), 6) + '0';
    SET @NewIssueNumber = STUFF(@NewIssueNumber, @StringIndex, 1, @CharacterAtStringIndex);
END

SELECT @NewIssueNumber;