My table 'LoadData' is as below:
Sequence_Code | ID | Current_Year | Record_Date| Status |
:------------ | -----: | -----------: | :----------| --|
17AA | 310001 | 2017 | 2017-01-01 | S
18AB | 310002 | 2018 | 2018-02-22 | S
19AC | 310003 | 2019 | 2019-02-10 | S
NULL | 310004 | 2019 | 2019-02-20 | A
I have written a SQL function 'GetNextSequenceCode_TEST' which generates the next sequence code,
For example: if we give 'AB' as Input, the function will generate 'AC' as output.
Sequence_Code is Alphanumeric,so i am supposed to consider last two Character of Sequence_Code .
Please look at the below code:
DECLARE @SeQCode varchar(100)
SELECT top 1 @SeQCode = SubString(Sequence_Code,3,4)
FROM LoadData where ID=310003
print @SeQCode
Here value of the variable @SeQCode i am getting is AC.
But when i send this variable to my function, like this:
select GetNextSequenceCode_TEST (@SeQCode )
it's giving output B!, which is wrong,
I am supposed to get AD as output.
And when i pass input like
select GetNextSequenceCode_TEST ('AC')
It gives me Correct output. i.e AD
Please correct me if i am doing something Wrong. or passign input in wrong way
What I have tried:
Function:
CREATE FUNCTION [dbo].[GetNextSequenceCode_TEST] (@LastSeqAlphabet varchar(100))
RETURNS varchar(100)
AS
BEGIN
DECLARE @fPart varchar(100), @lChar CHAR(1);
IF(@LastSeqAlphabet IS NULL OR @LastSeqAlphabet = '')
RETURN 'A';
SELECT @fPart = LEFT(@LastSeqAlphabet, LEN(@LastSeqAlphabet) - 1);
SELECT @lChar = RIGHT(@LastSeqAlphabet, 1);
IF(@lChar = 'Z')
RETURN (SELECT [dbo].[GetNextSequenceCode_TEST](@fPart)) + 'A';
RETURN @fPart + CHAR(ASCII(@lChar)+1);
END
I have tried this too:
select dbo.GetNextSequenceCode_TEST ('''' + @SeQCode + '''')
But getting a wrong output.