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

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.

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.

Attempting to determine decimals places using CHARINDEX, getting: Error converting data type varchar to float

I use this piece of code all the time on many objects when trying to determine the number of decimals after the '.'. The target system I am moving data to can only handle 2 d.p. So we I find data with >2 d.p. I multiply by 10, 100, 1000 and also multiply the 'price per' field value by the same factor.
SELECT
HQPROD, [HQPR1],
Decimals = CASE Charindex('.', [HQPR1])
WHEN 0 THEN 0
ELSE LEN(CAST(CAST(REVERSE(CONVERT(VARCHAR(50), [HQPR1], 128)) AS FLOAT) AS BIGINT))
END
FROM
[SM_REP].[dbo].[BPCSF64_HQT]) EDP ON P.HQPROD = EDP.HQPROD
The column datatype I am working with is DECIMAL(15,5).
Sample data:
HQPROD HQPR1
--------------------------
47001-RM64 175.89900
47001-T06OX 5.84500
47002-20T 80.44700
47002-T24 98.10300
47002-T32 144.07000
47003-01 1.54000
47003-02 1.54000
47003-03 0.51000
Error message:
Error converting data type varchar to float.
SELECT
HQPR1,
Decimals =
CASE WHEN HQPR1 > 0 THEN
CASE Charindex('.', [HQPR1])
WHEN 0 THEN 0
ELSE LEN(CAST(CAST(REVERSE(CONVERT(VARCHAR(50), [HQPR1], 128)) AS FLOAT) AS BIGINT))
END
ELSE
CASE Charindex('.', [HQPR1])
WHEN 0 THEN 0
ELSE LEN(CAST(CAST(REVERSE( Stuff((CONVERT(VARCHAR(50), [HQPR1], 128)),1,1,'' )) AS FLOAT) AS BIGINT))
END
END
This will work also for negative numbers, which as sticky bit said,could be causing your error.

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

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

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]