Is there a way to get the max value that can be stored in a bigint, without hardcoding it?
Is there a function or a constant that returns/contains this value?
Is there a way to get the max value that can be stored in a bigint, without hardcoding it?
Is there a function or a constant that returns/contains this value?
A bigint is always going to support
-2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807)
SQL Server TSQL does not define this as a constant, but it is always going to be -2^63 to 2^63 - 1
See the answer provided in this similar question. There is no way, as far as I know, to programmatically find the answer you're looking for.
Based on the comments you posted on another answer, this would allow you to only have to change your values in one place, as opposed to multiple places.
You could also create a simple user defined function that returns the max bigint value:
CREATE FUNCTION maxbigint() RETURNS bigint
AS
BEGIN
RETURN CAST(0x7FFFFFFFFFFFFFFF AS bigint)
END
GO
Then you can use it wherever you want by invoking it: dbo.maxbigint()
.