Arithmetic overflow error when summing an INT, how do I cast it as a BIGINT? - sql

When I try to get the sum of a column from a table I get the error Arithmetic overflow error converting expression to data type int because the resulting number is to big for an INT. So I tried to CAST to a BIGINT using the following
SELECT CAST(SUM(columnname) AS BIGINT) FROM tablename
This gives me the same error. Any ideas what i'm doing wrong?

Try converting it before summing. eg.
SELECT SUM(CONVERT(bigint, columnname)) FROM tablename
or
SELECT SUM(CAST(columnname AS BIGINT)) FROM tablename

Related

How to find which field is giving data type error in QUERY

I have the query report for user but have some error message display:
Error converting data type nvarchar to bigint
only one message.
How to find which field is giving data type error in query? Anyone can give advice? Many thanks!
As you have not provided data, it looks like you have NVARCHAR datatype, which is not implicitly convertable to Bigint
SELECT CAST(N'287888' AS BIGINT) -- Success
SELECT CAST(N'ABC123' AS BIGINT) -- Failure
See which column is failing and accordingly fix it.
You can either only load the proper values:
SELECT * FROM Table WHERE TRY_CAST(ErrorField AS BIGINT) IS NOT NULL
Or, You can load them as NULL(provided the target column allows NULL)
SELECT TRY_CAST(ErrorField AS BIGINT) FROM Table

Why does MS SQL CAST as bigint result in int overflow?

Why does the following T-SQL statement:
select CAST(COALESCE('3537601039',0) as bigint)
result in the following error when run against MS SQL Server 10?
The conversion of the varchar value '3537601039' overflowed an int
column.
Is there a "more correct" way to do this conversion of a varchar to a bigint that would avoid this problem when the number is within range of a bigint?
The first thing that is happening is that your string is trying to convert to a regular int to match the 0. Try this:
select CAST(COALESCE('3537601039','0') as bigint)
It appears that putting the unqualified zero in the coalesce is implying a conversion to a smaller int before it is explicitly recast to a bigint.
The following simple change to make it a coalesce of string values solves the problem:
select CAST(COALESCE('3537601039','0') as bigint)
Cast first, then do ISNULL
select ISNULL(CAST('3537601039' as bigint), 0)
select ISNULL(NULLIF(ISNULL(CAST('3537601039' as bigint), 0), 0), 1) + 3537601039
sql server firstly discovers isnull(string, numeric) and that numeric zero is by default int. So it tries to convert that string containing bigint to int. You were trying to cast to bigint too late.
another try:
select ISNULL('3537601039', 100000000000)

Syntax error while converting varchar to numeric in sql

I want to convert a varchar to numeric, I looked aroud and found SELECT CAST. I tried to use it like this this:
SELECT CAST(`Elo` AS INT) FROM Table `Rankings`;
However, I get a syntax error. Is there anything wrong?
Remove the Table keyword from your query.
SELECT CAST(`Elo` AS INT) FROM `Rankings`;

Data type varchar to numeric

I have a table with Varchar values that are numbers, and I want them to be decimal values. I'm getting an error saying Error converting data type varchar to numeric. I've tried:
SELECT ROUND(CAST(MYCOLUMN AS decimal(10, 2)), 2)
FROM TABLE
and
ALTER TABLE MYTABLE
ALTER COLUMN MYCOLUMN DECIMAL(10,2)
and
ALTER TABLE MYTABLE
MODIFY COLUMN ACQ_FIELD_8 DECIMAL(10,2)
and
SELECT ISNULL(CAST(NULLIF(MYCOLUMN, 'NULL') AS NUMERIC(10,2)), 0)
FROM MYTABLE
and I keep getting the same error. I looked the through the data to look for any special characters or letters by using:
SELECT MYCOLUMN FROM MYTABLE
WHERE MYCOLUMN LIKE '%[a-zA-Z]%'
or MYCOLUMN LIKE '%[(]%' --etc for each character.
The only thing that I have found is that every value for some reason has a '^' in it, but when I replace the '6' with nothing, the value still can't be converted to a decimal or numeric. If I try ordering the column by number (< 0.00), I get Arithmetic overflow error converting varchar to data type numeric.
Anyone know what to do?
Try this :
select round(try_convert(decimal(10,2), Mycolumn),2)
looks like you have some values which can not be converted to decimal. try_convert will convert them as NULL
You can try isnumeric:
SELECT ROUND(CAST(MYCOLUMN AS decimal(10, 2)), 2)
FROM TABLE
WHERE isnumeric(MYCOLUMN) = 1

Convert nvarchar to bigint in Sql server 2008

I want insert all rows of a table into another table, and I also want convert a nvarchar field into bigint, but when I use convert(bigint, col1) SQL Server shows an error:
Error converting data type nvarchar
to bigint
How can I fix this problem?
You could try to use ISNUMERIC to determine those rows that are indeed numeric:
UPDATE dbo.YourTable
SET BigIntColumn = CAST(NVarcharColumn AS BIGINT)
WHERE ISNUMERIC(NVarcharColumn) = 1
That would convert those rows that can be converted - the others need to be dealt with manually.
You should convert bigint to nvarchar not vice versa
cast(Other_Column_name as nvarchar) not cast (Column_Name as bigint)
you can try this:
CAST(CAST(col1 as NUMERIC) as BIGINT)
Here I convert navrchar value of column into bigInt and than perform Addition of thos two columns find blow :
SELECT (CAST(col1 AS BIGINT) + CAST(col2 AS BIGINT)) AS TotalValue from tableName