CASE statement fails with data conversion error - sql

I have a integer column in which i need to add a case statement to display in a report. If that int column = -1 , the data in that column should be DOWN else the int value / 100
If i try like below
CASE WHEN LOADVALUE = -1 then 'DOWN'
ELSE LOADVALUE/100 END
As expected, i'm receiving an error : Error converting varchar to int. Please help.

You need to cast your ELSE result to a VARCHAR
CASE WHEN LOADVALUE = -1 THEN 'DOWN'
ELSE CONVERT(VARCHAR (10), LOADVALUE/100) END
Something you should keep in mind though, is that since the datatype of LOADVALUE is an INT, you may get unexpected results due to integer division. You may want to consider using this instead:
CASE WHEN LOADVALUE = -1 THEN 'DOWN'
ELSE CONVERT(VARCHAR (10), (LOADVALUE * 1.0)/100.0) END
This will convert the LOADVALUE to a decimal to correctly do the division.
(Please disregard this alternative if you are actually wanting integer division)

When you use case make sure, the datatypes in when clause and else clauses are consistent.
CASE WHEN LOADVALUE = -1 then 'DOWN'
ELSE cast(LOADVALUE/100 as varchar) END

Related

SQL Select Case Conversion failed when converting the varchar value to data type int

With the help of case statement, I would like to get the result as "Non Revenue" in Geodept column where the value is 0
Simple case statement - (gives conversion error)
case when GeoDeptId = 0 then 'Non Revenue' else GeoDeptId end
So tried as below but getting value as 0 again
convert(int, case when IsNumeric(convert(int, rd.GeoDeptId ))= 0 then convert (varchar(14),'Non Revenue') else rd.GeoDeptId
end ),
So tried as below but getting value as 0 again
convert(int, case when IsNumeric(convert(int, rd.GeoDeptId ))= 0 then convert (varchar(14),'Non Revenue') else rd.GeoDeptId
end )
Cast the GeoDeptId to text:
CASE WHEN GeoDeptId = 0 THEN 'Non Revenue' ELSE CAST(GeoDeptId AS varchar(max)) END
The source of your error is that all branches of a CASE expression must have the same type. Since it is not possible for 'Non Revenue' to be cast to an integer, therefore we can convert the GeoDeptId to text.

Why is SQL Server trying to convert my nvarchar(20) datatype to an int?

I'm getting the "conversion" error in a SQL Select query.
The error is:
Msg 248, Level 16, State 1, Line 6
The conversion of the nvarchar value '7000952682' overflowed an int column.
Problem is, there are no int columns in my table!
Here is the table structure:
Here is the query:
If I set the value of #SU to NULL, then it does return all rows as expected. When the value is set to the string value of '7000952682' I get the error.
Why is SQL Server trying to convert the nvarchar value to an int?
All branches of a CASE expression have to have the same type. In this case (no pun intended), it looks like SQL Server is using an integer type and doing an implicit cast of SU to integer. The problem is that the max value for an integer in SQL Server is roughly 2.1 billion, and the example you gave is using the value 7000952682, hence the overflow.
You have two options here. You could make everything varchar:
CASE WHEN #SU IS NULL OR #SU = '' THEN '1' ELSE [SU] END
Or, you could make everything numeric, using a type that won't overflow, e.g.
CASE WHEN #SU IS NULL OR #SU = ''
THEN CAST(1 AS numeric(20, 6))
ELSE CAST([SU] AS numeric(20, 6)) END
As a side note, you could write the first part of your CASE expression more succinctly using COALESCE:
CASE WHEN COALESCE(#SU, '') = '' THEN '1' ELSE [SU] END
Don't use case in the where clause. The logic is more simply and accurately expressed as:
where (#su is null or #su = '' or #su = su)

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 inconsistent datatypes: expected number got char

I am trying to write a Case When statement, but I get an inconsistent datatypes error. I need 'Returned' displayed if the first when statement is not met.
CASE
WHEN (X.RECEIVED_QTY = 0) THEN FLOOR(SYSDATE-INVENTORY_TRANS.TRANSACTION_DATE)
WHEN (X.RECEIVED_QTY = 0) THEN 'RETURNED'
END AS DAYS_OUT
You can't mix results of a CASE statement, that means you can't return an INT under one condition and a VARCHAR in another. If you want to return Returned then you will need to CONVERT or CAST your numeric values to VARCHAR
You also have a syntactical problem with your CASE.
You'd have to change it to something like this:
CASE X.RECEIVED_QTY
WHEN 0 THEN CAST( FLOOR(SYSDATE-INVENTORY_TRANS.TRANSACTION_DATE) AS VARCHAR(20))
ELSE 'RETURNED'
END AS DAYS_OUT

SQL Server: casting the column datatype

SQL Server 2005/ 2008
I have kept a CASE condition for a SQL query which results Float /Numeric Value. One of the CASE condition is to say N/A. I kept the piece of code.
For column : Compliance - Possible values could be N/A,100.0,99.1 ... ( nvarchar, float ).
select
x.MemberName, x.DOB, x.FilePath,
x.Medication, x.NDC, x.Directions,
x.Name, x.Strength, x.GenericName,
x.QtyOrdered, x.DaysSupply, x.DateFilled,
CASE WHEN x.test = 0 THEN 'N/A'
WHEN compliance > 100.0 THEN '100.0'
ELSE CAST(FLOOR(compliance * 10)/10.0 AS DECIMAL(3, 1))
END AS [Compliance]
Above syntax spills error as ..
Msg 8114, Level 16, State 5, Line 10
Error converting data type varchar to numeric.
How should I type cast the field ?
CASE is an expression that returns a single result and regardless of the path it always needs to result in the same data type (or implicitly convertible to the same data type). Try:
...
CASE
WHEN x.test = 0 THEN 'N/A'
WHEN compliance > 100.0 THEN '100.0'
ELSE CONVERT(VARCHAR(5), CAST(FLOOR(compliance *10)/10.0 AS DECIMAL(3,1)))
END AS as [Compliance];
You're not consistent in your CASE statement - you need to provide the same data type as result for all options.
CASE WHEN x.test = 0 THEN 'N/A'
WHEN compliance > 100.0 THEN '100.0'
ELSE CAST(FLOOR(compliance * 10)/10.0 AS DECIMAL(3, 1))
END AS [Compliance]
Since the first two options return a string ('N/A' or '100.0'), your last option also must return a string value:
CASE WHEN x.test = 0 THEN 'N/A'
WHEN compliance > 100.0 THEN '100.0'
ELSE CAST(CAST(FLOOR(compliance * 10)/10.0 AS DECIMAL(3, 1)) AS VARCHAR(20))
END AS [Compliance]