Link to home
Start Free TrialLog in
Avatar of vbnetcoder
vbnetcoder

asked on

plus 1 to key

I have a field like this with values like this:  NJ001013205.  there are all the same length starting with NJ and then the numbers.  how would I get the mad field + 1

so, if the max value was this  NJ001013205 I would return  NJ001013206
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can use below -

DECLARE @rt AS VARCHAR(100) = 'NJ001013219'
SELECT 

	CASE WHEN CAST( RIGHT(@rt,1) AS INT ) < 9
		
		THEN SUBSTRING(@rt,0,LEN(@rt)) + CAST( CAST( RIGHT(@rt,1) AS INT ) + 1 AS VARCHAR)

	ELSE
			
		SUBSTRING(@rt,0,LEN(@rt)-1) + CAST( CAST( RIGHT(@rt,2) AS INT ) + 1 AS VARCHAR)

	END

Open in new window


Output

(No column name)
NJ001013220

Open in new window

Avatar of vbnetcoder
vbnetcoder

ASKER

ty