SQL Case statement error Msg 8114 - sql

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

Related

Format Float to N0 and convert to String

I have 3 columns, two are bit values, and one is a float. I want to combine these values into one column using the following case statement, I need the final column to be a string, so I can have text values representing the Bit values. This works fine, however, I also want to format the float as 'N0' before converting to a string. So a large number is shown as 1,234,567,890.
CASE
WHEN nd = 1 THEN 'ND'
WHEN nil = 1 THEN 'Nil'
ELSE LTRIM(STR(FORMAT(position, 'N0'))
END AS position
Msg 8114, Level 16, State 5, Procedure ChkSummary, Line 46 [Batch Start Line 152]
Error converting data type nvarchar to float.
EDIT:
DBMS SQL Server
As comments suggest ELSE FORMAT(position, 'N0') works.

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])
)

Arithmetic overflow error converting expression to data type smallint

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

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]