I have a CSV that I import into our database. One of the “columns” contains data that must be INT, but some rows have numbers that fall only in the BIGINT range (because this is test data from one of our partners). We keep INT inside of us and do not want to change.
I want to safely bring down BIGINT in INT. Safely, I mean that errors should not occur if arithmetic overflow occurs. If the cast / conversion succeeds, I want my script to continue. If he fails, I want him to be shorted. I can not imagine the correct syntax. This is what I have:
DECLARE @UserIDBigInt BIGINT = 9723021913; -- actually provided by query param --Setting within the INT range successfully converts --SET @UserIDBigInt = 5; DECLARE @UserID INT = CONVERT(INT, @UserIDBigInt); --DECLARE @UserID INT = CAST(@UserIDBigInt AS INT); SELECT @UserIDBigInt SELECT @UserID IF @UserID IS NOT NULL BEGIN SELECT 'Handle it as reliable data' END
I thought about comparing @UserIDBigInt with a valid INT range (-2 ^ 31 (-2,147,483,648) to 2 ^ 31-1 (2 147 483 647)), but I really don't like this approach. This is my failure. I was hoping for some language constructs or built-in functions that I could use. If I absolutely need to compare with a valid range, are there at least some built-in constants (e.g. C # int.MinValue and int.MaxValue)?
EDIT : typo fixed.
Olson.dev
source share