Question

I have data in my CaseFiles table csfl_CaseNo column as

00001/13-14
00002/13-14
00003/13-14
00004/13-14
00005/13-14

Here is my stored procedure to increment the number

ALTER PROCEDURE [dbo].[HLA_CaseNo](@strFnyr as nvarchar(5),@strSuffix as nvarchar(15) OUTPUT) 
AS
BEGIN
--Declarations
declare @strSuffix1 as nvarchar(15)
declare @strTmp as nvarchar(5)
declare @intNew as int
--declare @strSql as nvarchar(2000)
set @strSuffix1 = null  
  --Process
begin
    set @strSuffix1 = (select max(csfl_caseNo) from CaseFiles where csfl_Fynyr = @strFnyr)
    --set @strPrf='QUT'
end

if @strSuffix1 is null
    set @intNew = 1
else
    set @intNew = cast(substring(@strSuffix1,0,5) as int) + 1

print 'The value of intNew ' + cast(@intNew as nvarchar(10))
set @strTmp = cast(@intNew as nvarchar(5))
set @strTmp = (select replicate('0',5-len(@strTmp)) + @strTmp)
set @strSuffix = @strTmp + '/' + @strFnyr   

END

My task is when I pass the financial year as 13-14 it has to return the value as 00006/13-14, but it is returning 00002/13-14 instead.

Why this is happening? Please help

Was it helpful?

Solution

Change

set @intNew = cast(substring(@strSuffix1,0,5) as int) + 1

to

set @intNew = cast(substring(@strSuffix1,1,5) as int) + 1

OR even rather use

LEFT(@strSuffix1,5)

rather than

substring(@strSuffix1,1,5)

so, something like

set @intNew = cast(LEFT(@strSuffix1,5) as int) + 1

From SUBSTRING (Transact-SQL)

start

Is an integer or bigint expression that specifies where the returned characters start. If start is less than 1, the returned expression will begin at the first character that is specified in expression. In this case, the number of characters that are returned is the largest value of either the sum of start + length- 1 or 0. If start is greater than the number of characters in the value expression, a zero-length expression is returned.

So that would imply start + length- 1 = 0 + 5 - 1 = 4, which would imply cast(substring(@strSuffix1,0,5) as int) is 0, and set @intNew = cast(substring(@strSuffix1,0,5) as int) + 1 = 1

OTHER TIPS

My bet is that...

set @strSuffix1 = (select max(csfl_caseNo) from CaseFiles where csfl_Fynyr = @strFnyr)

...is setting @strSuffix1 to null because no there is no CaseFiles record where csfl_Fynyr = @strFnyr.

Check that you are trimming csfl_Fynyr and @strFnyr if appropriate - possible mismatch on trailing whitespace you have missed.

To debug just that statement, take the value that you are passing for @strFnyr and replace @strFnyr in the statement with the value in single quotes: run it on its own to see whether you actually get a max(csfl_caseNo) value back.

You can make it more succinct with one statement

SELECT RIGHT('0000' + CAST(LEFT(MAX(csfl_CaseNo), 5) + 1 AS VARCHAR(5)), 5) + '/13-14'
  FROM CaseFiles
 WHERE RIGHT(csfl_CaseNo, 5) = '13-14';

Then your SP might look like

CREATE PROCEDURE HLA_CaseNo(@strFnyr as nvarchar(5), @strSuffix as nvarchar(15) OUTPUT) 
AS
SELECT @strSuffix = RIGHT('0000' + CAST(LEFT(MAX(csfl_CaseNo), 5) + 1 AS VARCHAR(5)), 5) + '/' + @strFnyr
  FROM CaseFiles
 WHERE RIGHT(csfl_CaseNo, 5) = @strFnyr;

SQLFiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top