In SQL Server 2008 I have a table with similar structure. Unfortunately changing the data structure is not an option, so I am stuck with attempting to come up with an alternative. I have tried to run this query, but I get an error of:
(5 row(s) affected)
Msg 8114, Level 16, State 5, Line 9
Error converting data type varchar to numeric.
Here is DDL – what syntax should be changed in the query in order for me to receive valid output and not an error message?
Declare @GreenHouse Table
(nomen varchar(100), vtc varchar(100), d1 date)
Insert Into @GreenHouse (nomen, vtc, d1) VALUES
('Green', '507.02', '2016-01-14'), ('Green', '4.44089e-015', '2016-01-03')
,('Green', '200.57', '2016-01-18'), ('Green', '649.01', '2016-01-19'),
('Green', '1849.85', '2016-04-30')
Select nomen, Round(Sum(Cast(vtc As Decimal(10,2))),0.00) As FormatedInfo, d1
FROM @GreenHouse Group By nomen, d1 Order By d1 DESC
Best Answer
The
4.44089e-015
is most likely giving you the error by trying to convert it to adecimal
. Try usingfloat
instead:The results with your sample data are:
(yeah, I know that it converts the
4.44089e-015
value to0
, but it is expected for a float)