SQL Server 2005 - Converting failure - sql

I'm selecting values from a table. Inside the table there is a column called value. I'm trying to select that value with a 'g' at the end of the value when the value is a number. However, when i try to do this i get the following error (see below).I think this is because i'm using ISNUMERIC, and since the values are either a string representation of a number or the string value 'None' (i know weird, but i have to have this way), i get a convert failure. Anyone know how to fix this issue in SQL Server 2005
Error
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'None' to data type int.
Query
SELECT a.student_assignment_id,
a.student_id,
a.location_id,
a.assignment_type,
(
Case
WHEN a.assignment_type = 'HW' THEN
CASE a.value
WHEN ISNUMERIC(a.value) THEN RTRIM(a.value)+'g'
ELSE a.value
END
ELSE a.value
END
) as value
,
a.start_date,
a.end_date,
a.course,
a.created_by,
a.creation_date,
a.modified_by,
a.modified_date,

You need to use CASE WHEN ISNUMERIC(a.value) = 1 instead of CASE a.value WHEN ISNUMERIC(a.value)
CASE WHEN a.assignment_type = 'HW'
THEN
CASE WHEN ISNUMERIC(a.value) = 1
THEN RTRIM(a.value)+'g'
ELSE a.value
END
ELSE a.value
END
ISNUMERIC() returns a boolean 0 (not numeric) or 1 (numeric), so your current query is attempting to compare the string a.value to an int (0 or 1)

Related

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)

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

Adding text string to CASE Statement

I am using the following SQL CASE:
SELECT
BomMast.BomStockCode
, BomMast.BomDescription
, CASE
WHEN StkItem.AveUCst <= 0 THEN 'ERROR'
WHEN StkItem.AveUCst > 0 THEN (StkItem.AveUCst * BomComp.ProductionQty)
END AS TotalCost
FROM BomComp
INNER JOIN BomMast
ON BomMast.BomID = BomComp.BomMasterKey
INNER JOIN StkItem
ON StkItem.StockLink = BomComp.ComponentStockLink
But I get the following message:
Msg 8114, Level 16, State 5, Line 2
Error converting data type varchar to float.
Am I not allowed to add test within the CASE statement?
Thank you!
Change your query to:
SELECT BomMast.BomStockCode
,BomMast.BomDescription
,CASE
WHEN StkItem.AveUCst <= 0
THEN 'ERROR'
WHEN StkItem.AveUCst > 0
THEN CAST((StkItem.AveUCst * BomComp.ProductionQty) AS NVARCHAR(MAX))
END AS TotalCost
FROM BomComp
INNER JOIN BomMast ON BomMast.BomID = BomComp.BomMasterKey
INNER JOIN StkItem ON StkItem.StockLink = BomComp.ComponentStockLink
The datatypes of the values you want to show in either branches of your CASE statements need to be the same in order to work.
Edit:
After #underscore_d's suggestion, I also consider that it would be a far better option to display NULL instead of the message ERROR and then handle this NULL value in the application level.
Hence, your case statement will change to:
CASE
WHEN StkItem.AveUCst <= 0
THEN NULL
WHEN StkItem.AveUCst > 0
THEN (StkItem.AveUCst * BomComp.ProductionQty)
END AS TotalCost
Yes, text can be used as the result a case statement, as can any datatype, but each case must return the same type, as the results column must have one type only.
Your [TotalCost] column has conflicting data types. [StkItem.AveUCst] is a float and the literal value of 'ERROR' is a varchar. If you are intending to retain the benefits of number-based value in your results column, consider replacing 'ERROR' with the SQL keyword NULL.
Your column TotalCost (neither any other column) can be a type-mixture. In first case it would be a varchar, in second case it would be floator something like that. THAT IS NOT POSSIBLE.

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

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]