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

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.

Related

NULL check in WHEN statement is not working in SQL Server

I expect 'No Conversion' should be printed for the below code but instead receiving the following error 'Error converting data type varchar to numeric.'
When the TRY_PARSE inside the WHEN statement returns null then the THEN block should execute?
DECLARE #VALUE varchar(20) = 'NA'
SELECT
CASE
WHEN TRY_PARSE(#VALUE AS DECIMAL) IS NULL
THEN 'No Conversion'
ELSE (CONVERT(DECIMAL(10, 3), #VALUE, 0))
END
All branches of a CASE expression need to have the same type. Given that the error message is text, you should also be including the cast decimal value as text, something like this:
SELECT CASE WHEN TRY_PARSE(#VALUE AS DECIMAL) IS NULL
THEN 'No Conversion'
ELSE CONVERT(varchar(max), CONVERT(decimal(10,3), #VALUE, 0)) END

Casting a String Column to 3 Digit scale in Hive Query Language

I have a select statement as follows
SELECT t5.Name AS ProductName
, CAST (t5.salerevenue AS DECIMAL(20,3)) AS Revenue
, CASE WHEN t5.PreviousRevenue <> 0 THEN CAST(t5.PresentRevenue/t5.PreviousRevenue*100 AS STRING)
ELSE 'NA'
END AS RevenueTrend
I want to cast the RevenueTrend as a decimal with maximum 3 point scale (20.123)
I have to cast it as String because WHEN t5.PreviousRevenue <> 0 is not met I have to show it as 'NA'
I tried doing it as follows
CASE WHEN t5.PreviousRevenue <> 0 THEN CAST((CAST(t5.PresentRevenue/t5.PreviousRevenue*100) AS DECIMAL(20,3)) AS STRING)
ELSE 'NA'
END AS RevenueTrend
but I am getting a syntax error.
The revenue part is projected as expected. I want the revenuetrend part also to be projected like revenue. Is there any way to achieve this?
Try converting first to a decimal, then to a string:
SELECT . . .
(CASE WHEN t5.PreviousRevenue <> 0
THEN CAST(CAST(t5.PresentRevenue/t5.PreviousRevenue*100 AS DECIMAL(20,3)) as string)
ELSE 'N/A'
END) AS RevenueTrend

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.

CASE statement fails with data conversion error

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

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]