Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to select a record
SQL
ISNULL( MAX(CAST(RIGHT('012016031800001',15) AS VARCHAR(15))), 0) + 1


this gives an error like

The conversion of the varchar value
JavaScript
'012016031800001'

overflowed an int column.

What I have tried:

SQL
SELECT ISNULL( MAX(CAST(RIGHT('012016031800001',15) AS VARCHAR(15))), 0)+1
Posted
Updated 7-Jun-20 8:13am
v2

Well...yes.
The max value you can get in an INT is 2147483647
And you are trying to add one to:
C#
012016031800001
     2147483647
See the problem?
I'm not exactly sure why you are doing that: the VARCHAR cast is already unnecessary, and the ISNULL is also pretty redundant by that point.
This may be what you need:
SQL
SELECT MAX(CAST(RIGHT('012016031800001',15) AS BIGINT))+1


But...If you are trying to use this to generate unique IDs in advance, that's a bad idea!
 
Share this answer
 
v2
you are trying to add a varchar value with an integer value
try this

SQL
DECLARE @MAXCODE varchar(15);
SELECT  @MAXCODE=ISNULL( MAX(CAST(RIGHT('012016031800001',15) AS VARCHAR(15))), 0) +'1'
PRINT @MAXCODE
print len( @maxcode)


SQL
DECLARE @MAXCODE varchar(16);
SELECT  @MAXCODE=ISNULL( MAX(CAST(RIGHT('012016031800001',15) AS VARCHAR(15))), 0) +'1'
PRINT @MAXCODE
print len( @maxcode)
 
Share this answer
 
SQL
CONVERT(BIGINT, ISNULL( MAX(CAST(RIGHT('022016031800001',15) AS VARCHAR(15))), 0))+1
 
Share this answer
 
v2
The max data value of in is
2147483648


how to get the max value of int.
Select Power(cast(2 as varchar),(32) -1) as 'int max range'  from sys.types Where name = 'Int' 


you can you BIGINT instead of Int
SELECT MAX(CAST(RIGHT('012016031800001',15) AS BIGINT))+1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900