Why does this give `error converting data type varchar to numeric` - sql

I am having a SQL issue, and I have boiled it down to this:
if isnull(5.5,'') = isnull(null,'') select 0 else select 1
which throws the error Error Converting data type varchar to numeric.
Why is this throwing this error, and how to resolve?

This simpler expression (which is equivalent to your expression) gives the same error:
if 5.5 = '' select 0 else select 1
You're comparing a varchar to a numeric type as the error indicates.
You can CAST your expression to match types:
if CAST(isnull(5.5,'') AS VARCHAR) = isnull(null,'') select 0 else select 1

Related

SQL error in CASE expression

SELECT DISTINCT
AM.programid,
AM.ProjectNumber,
AM.AppID,
AM.DateCreated,
CASE WHEN AM.applicationentryflag = 210
AND YEAR(App646.Application_Received_Date) = CONVERT(INT,#PY)
THEN CAST(CONVERT(VARCHAR(10),App646.Application_Received_Date,101) AS DATETIME)
WHEN AM.applicationentryflag <> 210
AND YEAR(AM.datecreated) = CONVERT(INT,#PY)
THEN CAST(CONVERT(VARCHAR(10),AM.datecreated,101) AS DATETIME)
END AS [Date_App_Rec]
I get the following error when executing this code:
At least one of the result expressions in a CASE specification must be
an expression other than the NULL constant.
This error occurs when SQL can't infer a type, which means that all results in one of your case expressions are null for example:
SELECT CASE WHEN 1 = 0 THEN NULL ELSE NULL END
I tested your Case statement with a static date and it doesn't seem to have a problem. (However, I am not sure why you would convert date to varchar(10) then back to DATETIME)
SELECT CAST(CONVERT(VARCHAR(10),'2017-08-31',101) AS DATETIME)
SELECT CAST(CONVERT(VARCHAR(10),'2017-08-31',101) AS DATETIME)
so I doubt the error is from the code you have shown us.

even after converting it to varchar it is giving error: Error converting data type varchar to float,

SELECT a.[Emp_No],
a.[Emp_Name],
a.[Band],
b.[Max_Exp] AS Max_Yrs_Experience,
CASE WHEN a.Performance_Score = 0 AND (a.Status_Active > 0 OR a.Status_New > 0)
THEN CONVERT(varchar, 0)
WHEN a.Performance_Score > 0
THEN [Performance_Score]
ELSE 'No History Data Found'
END AS Performance_Score
INTO #EXPER
FROM #PERFORMANCE a, #agg_exp b
WHERE a.[Emp_No] = b.[Emp_No]
ORDER BY Performance_Score DESC
Error converting varchar to float.
I don't know why I am getting this error.
The difficulty you are having is due to that SQL Server requires that all posssible values generated in a CASE expression have the same type. Since your ELSE uses text, which can't be converted to any numeric type, the only option is to convert the numbers to text.
CASE WHEN a.Performance_Score = 0 AND (a.Status_Active > 0 OR a.Status_New > 0)
THEN CONVERT (VARCHAR(10), 0) -- zero as text
WHEN a.Performance_Score > 0
THEN CONVERT (VARCHAR(10), [Performance_Score]) -- performance score as text
ELSE 'No History Data Found' -- this is already text
END AS Performance_Score
Going by the error message, I am assuming here that [Performance Score] is a float column. If it be some other type, you would have to modify the call to CONVERT() appropriately.

Sql ISNUMERIC()

SELECT CASE WHEN ISNUMERIC('1a') =1 THEN 1 ELSE 'A' END
i'am getting this error !!
Conversion failed when converting the varchar value 'A' to data type int.
you are a victim of data type precedence.Taken from BOL:
When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence. If the conversion is not a supported implicit conversion, an error is returned. When both operand expressions have the same data type, the result of the operation has that data type
So in your case ,CASE is an expression and when two or more data types are combined in this expression,it returns data type of highest precedence..in your case it is INT..So change your query to below
SELECT CASE WHEN ISNUMERIC('1a') =1 THEN cast(1 as varchar) ELSE 'A' END
SELECT CASE WHEN ISNUMERIC('1a') =1 THEN 1 ELSE 2 END
SELECT CASE WHEN ISNUMERIC('1A') = 1 THEN '1' ELSE 'A' END
OR
SELECT CASE WHEN ISNUMERIC('1A') =1 THEN '1' ELSE SUBSTRING('1A',2,2) END

Conversion failed when converting the nvarchar value 'These are comments for errors' to data type int

I have been struggling to get this corrected for a while now. Below is the part of a query where I am getting the error in the MAX function and I am not able to understand why its trying to convert to data type INT. I would like to keep it as string as this particular column is for comments
MAX(CASE
WHEN ID = -416 AND Value IS NOT NULL
THEN Value
ELSE 'No Error'
END) AS Error_Comments
This particular table is still in test and hence for test purposes there is only one comment added for now which is
These are comments for errors
I also tried converting the case statement but it still gives the same error:
MAX(CONVERT(VARCHAR, CASE
WHEN ID = -416 AND Value IS NOT NULL
THEN Value
ELSE 'No Error'
END)) AS Error_Comments
I also tried this but didn't work: Value <> ''
I couldn't find the answer in any of the stackoverflow questions.
Try this:
declare #t table(ID int, value nvarchar(1000))
insert #t values(-416, 'These are comments for errors'),
(-416, null)
SELECT
COALESCE(MAX(CASE WHEN ID = -416 THEN value END), 'No Error') AS Error_Comments
FROM #t
Result:
These are comments for errors
It would seem that value is an integer. So, your case statement is returning different types, and SQL Server has decided that the type should be an integer. Your string doesn't convert.
So, do an explicit conversion:
MAX(CASE WHEN ID = -416 AND Value IS NOT NULL THEN cast(Value as varchar(255))
ELSE 'No Error'
END) AS Error_Comments
EDIT:
The comment makes a lot of sense. I suppose this should be:
COALESCE(CAST(CASE WHEN ID = -416 AND Value IS NOT NULL THEN VALUE END) as varchar(255)),
'No Error'
)

select case statement error in mssql

SELECT top 1
case
when VR = -99999.99
then 0
else cast((VR*1.732) as decimal(38,3))
end
FROM pseb.dbo.datasource
where FeederID=5003
order by datetime desc
The above query is working fine, but I need to return varchar value '--' instead of returning 0
if I do like that
SELECT top 1
case
when VR = -99999.99
then '--'
else cast((VR*1.732) as decimal(38,3))
end
FROM pseb.dbo.datasource
where FeederID=5003
order by datetime desc
means it returns the following error:
Msg 8114, Level 16, State 5, Line 1 Error converting data type varchar
to numeric.
please help me to solve it
The problem is that you are returning two different data types from the same column. The rule with SQL Server that numeric types take precedence over string types, i.e. in a situation like yours a string gets converted to a number, not the other way around.
So to solve this you can cast your number to a string.
One option is to do something like this:
SELECT top 1
case when VR = -99999.99 then '--'
else
cast
(
cast((VR*1.732) as decimal(38,3)
)
as varchar(50))
end
FROM pseb.dbo.datasource where FeederID=5003 order by datetime desc