Reputation: 383
I cannot seem to figure out a way to cast a calculated column from a SQL Server 2012 SELECT
statement. I'm calculating a simple value called Yield that is basically a percentage.
SELECT
TotalUnique,
TotalFail,
ISNULL(((TotalUnique - NullIf(TotalFail, 0)) / CONVERT(NUMERIC(38, 2), TotalUnique) * 100), 100) AS Yield
FROM
Products
For the Yield
column how would I retrieve this with a reader? I keep getting the error message
Exception Details: System.InvalidCastException: Specified cast is not valid.
on the line:
DBYield = (float)reader["Yield"]
I've looked all day for something that would provide me a clue but have had no luck.
int DBTotalUnique = 0;
int DBTotalFailed = 0;
float DBYield = 00.0f;
while (reader.Read())
{
DBTotalUnique = (int)reader["TotalUnique"];
DBTotalFailed = (int)reader["TotalFail"];
DBYield = (float)reader["Yield"]
}
Could someone please give me a hint?
Upvotes: 1
Views: 78
Reputation: 3777
Numeric
in TSQL is equivalent to Decimal
in .NET (see this article for the data type mappings from TSQL to .NET).
Try casting to a decimal
.
Upvotes: 3