Arithmetic overflow error converting expression to data type smallint - sql

I am new to SQL Server, this question might be repeated here. Since i haven't find a solution for my problem. I wished to post here. So here is my problem
select(volume * speed) from traffic_data_replica;
I am trying to multiply values from two columns , data type is smallint for both columns. Error i got is :
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type smallint.

Cast one of the values to a "bigger" type before the calculation:
select cast(volume as int) * speed
from traffic_data_replica;
You can also do this easily by multiplying by 1.0:
select 1.0*volume*speed
from traffic_data_replica

Related

Trying to pull unique values using with statement

I created a straight forward table, but wanted to incorporate a with operator to pull the unique 'pulses' from it. This is a label in my database. I tried the below but got an unknown error.
Error:
09:58:45Started executing query at Line 1 Msg 8115, Level 16, State 2,
Line 3 Arithmetic overflow error converting expression to data type
int. Warning: Null value is eliminated by an aggregate or other SET
operation. Total execution time: 00:00:22.736
with temp_table as
(
SELECT distinct(b.creative_id)
,b.creative
,channel
,publisher
,strategy
,pulse
,sum(a.impressions) as cpi_imps
,sum(a.clicks) as cpi_clicks
,avg(a.cpi) as Avg_CPI
,max(a.cpi) as max_cpi
FROM [digital_media].[dbo].[tableau_out_combined] as b
LEFT JOIN digital_media.dbo.in_encore_creative_v3 as a
on b.creative_id = a.creative_id
where channel = 'Performance Display'
and date between '2019.12.01' and '2019.12.31'
and (b.creative_id is not null and b.creative_id <> 0)
group by channel, publisher,strategy, b.version, b.creative_id, b.creative, pulse
)
select distinct(pulse)
from temp_table
I'm certain I am using it incorrectly. Any guidance greatly appreciated.
I originally put this into a comment but I am certain that it is the required answer.
Either:
You are applying your sum to a non-numeric column which is causing problems that can only be solved through data cleaning.
Your b.creative_id column is not an int datatype and is falling foul of the implicit conversion created by your and (b.creative_id is not null and b.creative_id <> 0) comparison to the int value of 0. This is remedied by using '0' instead.
Or the total value of your sum is larger than the int datatype can handle (2,147,483,647). To remedy this, convert your int values to bigint values before passing them to the sum, so that the sum returns a bigint data type that can hold the resulting value:
,sum(cast(a.impressions as bigint)) as cpi_imps
,sum(cast(a.clicks as bigint)) as cpi_clicks

SQL Code Error converting data type varchar to float

The following code encounters an error when executed in Microsoft Server Management Studion:
USE [DST]
GO
Select
CAST([Balance] as float)
FROM [RAW_XXX]
WHERE ISNUMERIC(Balance) = 1
Msg 8114, Level 16, State 5, Line 2
Error converting data type varchar to float.
I thought that the ISNUMERIC would exclude anything that can not be cast or converted.
It is a massive database in SQLServer 2012 so I am unsure how to find the data that is causing the error.
Use TRY_CONVERT to flush out the offending records:
SELECT *
FROM [RAW_XXX]
WHERE TRY_CONVERT(FLOAT, Balance) IS NULL;
The issue with your current logic is that something like $123.45 would be true according to ISNUMERIC, but would fail when trying to cast as floating point.
By the way, if you wanted a more bare bones way of finding records not castable to float you could just rely on LIKE:
SELECT *
FROM [RAW_XXX]
WHERE Balance NOT LIKE '%[^0-9.]%' AND Balance NOT LIKE '%.%.%';
The first LIKE condition ensures that Balance consists only of numbers and decimal points, and the second condition ensures that at most one decimal point appears. Checkout the demo below to see this working.
Demo

Char to Decimal

I am trying to multiply two fields together. Problem is, one is CHAR and one is DECIMAL.
OEAUDD.QTYSHIPPED = DECIMAL
LITERS.[VALUE] = CHAR
(This column holds more than just numbered values so I'm not able to permanently change the data type in that column. However, all of the data I'm pulling is numerical only and I need to be able to multiply it by qtyshipped)
Original code:
CONVERT (DOUBLE PRECISION, TABLENAME.QTYSHIPPED*LITERS.[VALUE]) AS 'TotalLitersSold'
This was working fine for a while and then suddenly started throwing error:
Msg 8114, Level 16, State 5, Line 3
Error converting data type varchar to numeric
Attempted:
CONVERT (DECIMAL,LITERS.[VALUE])
and got the same error.
Attempted:
CAST (LITERS.[VALUE] AS DECIMAL)
and got the same error.
Is there any way to change the data type of this column to decimal so I can multiply the two values together?
Thank you in advance for the assistance.
Use TRY_CONVERT() and use it on each value separately:
(TRY_CONVERT(DOUBLE PRECISION, TABLENAME.QTYSHIPPED) *
TRY_CONVERT(DOUBLE PRECISION, LITERS.[VALUE])
)

Operand data type nchar is invalid for avg operator

I have a table that saves the size of file in the database when the user uploads it. I want to get the average value of all the size that the user uploaded.
I have the following column as example that shows the size in Mb's
|Size|
|1.20|
|0.25|
|0.50|
The result that I want as average is something like this
|Size|
|0.65|
When I'm trying to get the average I get this error
Msg 8117, Level 16, State 1, Line 15 Operand data type nchar is
invalid for avg operator.
EDIT
I have changed the column type to nvchar and get this error message when I'm converting it to int
Conversion failed when converting the nvarchar value '0,24' to data
type int.
When I tried it with a decimal I get this error message
Msg 8114, Level 16, State 5, Line 11
Error converting data type nvarchar to numeric.
What can I do to fix this problem.
The error shows you have 0,24 as a number in Swiss or German format.
This is different to 0.24 in British or US format.
So if the datatype was correct as decimal or float, 0,24 would not be allowed because SQL Server does not really deal with continental number formats. See SQL server with german regional settings for more
Then, neither is integer either of course so the int conversion fails.
So, fix the column datatype and the data to fix the error. And you can also avoid nasty client number to string conversions like 24E-2 which is only recognised as float.
And what if you have thousand separators too with mixed format? Imagine this dataset
123.456,79
234,567.89
34E5
0,24
0.24
2.3E-1
Fixing the data will require some LIKE searches. At least these to fix each format one by one.
LIKE '%,%.%'
LIKE '%.%,%'
LIKE '%,%'
LIKE '%E%'
0.24 won't convert to an int as it has decimal part.
You need to do CAST([size] as DECIMAL(9,2)) or some such...
Although we could really do with seeing your code :)
To use this CAST as part of an aggregate...
SELECT [database], AVG(CAST([size] as DECIMAL(9,2))) AS [Average of Size]
FROM table
GROUP BY [database]
Of course I don't know what your table or query actually is...
As others have said - if you are are going to be taking the average anyway then you will be better off not converting the number to an NVARCHAR or VARCHAR in the first place and working with the plain numeric fields.
As gbn notes this is 0,24 continental format so...
SELECT [database], AVG(CAST(REPLACE([size],',','.') as DECIMAL(9,2))) AS [Average of Size]
FROM table
GROUP BY [database]

SQL Case statement error Msg 8114

Trying to build a simple case statement using SQL Server 2008 and running across an issue giving me the following error message:
Msg 8114, Level 16, State 5, Line 6
Error converting data type varchar to numeric.
I've isolated the script causing the issue to the following:
CASE WHEN tPersonLocationHist.LocationCode = 'DC'
AND (tPersonJobHist.FullTimeEquivalent = 1)
AND (MONTH(tPersonStatusHist.LatestHireDate) < 10)
THEN tPersonStatusHist.NormalHoursPerWeek / 5
The tPersonStatusHist.NormalHoursPerWeek is formatted as decimal; however, I can't get it to calculate.
Any tips? The resulting calculation needs to be in decimal form (to two decimal digits).
Even if I change the THEN statement to just '7.5', it then returns:
Msg 245, Level 16, State 1, Line 6
Conversion failed when converting the varchar value '7.5' to data type int.
I've tried using CONVERT(decimal(4,2),tPersonJobHist.NormalHoursPerWeek * 7.5), but no luck on that either.
Only thing that's working is to do it as:
CONVERT(int,tPersonJobHist.NormalHoursPerWeek * 7.5), but then it is dropping the decimals and just giving the whole integer.
As you can probably tell, I'm new into SQL and still learning the rope, so any help you can give is much appreciated.
Are your other cases/else returning varchar? They should all return the same datatype. You may want to cast/convert them to be sure.
CAST(CONVERT(decimal(4,2),tPersonJobHist.NormalHoursPerWeek * 7.5) AS varchar(50))
First convert to decimal then do the division:
THEN CONVERT(decimal(4,2), tPersonStatusHist.NormalHoursPerWeek) / 5
Or cast is an option:
THEN CAST(tPersonStatusHist.NormalHoursPerWeek AS decimal(4,2)) / 5