| Author |
Topic |
|
dcarva
Posting Yak Master
140 Posts |
Posted - 2008-05-05 : 11:32:57
|
| Hello,I have the following query that runs against a view:select cast((convert(varchar, HD.Value)) as float)from HardwareData HDwhere (HD.PropertyName = 'Total Space' and HD.CategoryName = 'Disk Drive(s)')This query works with no errors. When I add a where clause as follows, it breaks:select cast((convert(varchar, HD.Value)) as float)from HardwareData HDwhere (HD.PropertyName = 'Total Space' and HD.CategoryName = 'Disk Drive(s)')and cast((convert(varchar,HD.Value)) as float) > 9999999I get a "Msg 8114, Error converting data type varchar to float." message. Why would the statement in the WHERE clause fail to convert to float and not the statement that's in the select statement? It's as if the where clause conversion is done against all of the data, not just against the subset specified by the first part of the where clause where I try to narrow it down by total space. Any ideas?THANKS |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-05 : 11:35:45
|
quote: Originally posted by dcarva Hello,I have the following query that runs against a view:select cast((convert(varchar(length), HD.Value)) as float)from HardwareData HDwhere (HD.PropertyName = 'Total Space' and HD.CategoryName = 'Disk Drive(s)')This query works with no errors. When I add a where clause as follows, it breaks:select cast((convert(varchar(length), HD.Value)) as float)from HardwareData HDwhere (HD.PropertyName = 'Total Space' and HD.CategoryName = 'Disk Drive(s)')and cast((convert(varchar(length),HD.Value)) as float) > 9999999I get a "Msg 8114, Error converting data type varchar to float." message. Why would the statement in the WHERE clause fail to convert to float and not the statement that's in the select statement? It's as if the where clause conversion is done against all of the data, not just against the subset specified by the first part of the where clause where I try to narrow it down by total space. Any ideas?THANKS
Always remember to specify a length value while converting to varchar type. |
 |
|
|
dcarva
Posting Yak Master
140 Posts |
Posted - 2008-05-05 : 11:38:07
|
| Thank you. However I still get the error with length specified. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-05 : 11:40:38
|
quote: Originally posted by dcarva Thank you. However I still get the error with length specified.
What does HDValue contain? Why are converting it to varchar and again casting it to float? |
 |
|
|
dcarva
Posting Yak Master
140 Posts |
Posted - 2008-05-05 : 11:44:45
|
| Let me simply the query a bit:This works:select cast(HD.Value as float)from HardwareData HDwhere (HD.PropertyName = 'Total Space' and HD.CategoryName = 'Disk Drive(s)')This does not:select cast(HD.Value as float)from HardwareData HDwhere (HD.PropertyName = 'Total Space' and HD.CategoryName = 'Disk Drive(s)')and cast(HD.Value as float) > 9999999The HD.Value column contains alphanumeric data. However, the first query above only brings back numeric data because of the where clause narrowing it down to 'Total Space' type. Therefore, I don't understand why the second query fails because it should be narrowing it down to only bringing back numeric data. If it was returning alphanumeric data, then the first query would fail.Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-05 : 11:55:06
|
quote: Originally posted by dcarva Let me simply the query a bit:This works:select cast(HD.Value as float)from HardwareData HDwhere (HD.PropertyName = 'Total Space' and HD.CategoryName = 'Disk Drive(s)')This does not:select cast(HD.Value as float)from HardwareData HDwhere (HD.PropertyName = 'Total Space' and HD.CategoryName = 'Disk Drive(s)')and cast(HD.Value as float) > 9999999The HD.Value column contains alphanumeric data. However, the first query above only brings back numeric data because of the where clause narrowing it down to 'Total Space' type. Therefore, I don't understand why the second query fails because it should be narrowing it down to only bringing back numeric data. If it was returning alphanumeric data, then the first query would fail.Thanks
can you try like this and see if it errors?select cast(HD.Value as float)from HardwareData HDwhere (HD.PropertyName = 'Total Space' and HD.CategoryName = 'Disk Drive(s)')and (isnumeric(HD.Value)=1 and cast(HD.Value as float) > 9999999) |
 |
|
|
dcarva
Posting Yak Master
140 Posts |
Posted - 2008-05-05 : 11:57:40
|
| Yes, I tried that as well. It still errors out. It's very strange. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-05-05 : 11:58:13
|
You can't garuentee which order SQL will evalute expressions. So, in your second case it is evaluating "and cast(HD.Value as float) > 9999999" before it evaluates the other expressions and thus why it is failing. YOu might try something like this, but SQL might try to be smart and colapse the statement into the same thing as you have above: SELECT *FROM( select cast(HD.Value as float) AS Vaa from HardwareData HD where (HD.PropertyName = 'Total Space' and HD.CategoryName = 'Disk Drive(s)'))WHERE Val > 9999999 |
 |
|
|
dcarva
Posting Yak Master
140 Posts |
Posted - 2008-05-05 : 12:03:03
|
I have also tried a subquery like that. It also fails the same way. Driving me crazy. |
 |
|
|
dcarva
Posting Yak Master
140 Posts |
Posted - 2008-05-05 : 12:22:31
|
| This also fails with the "varchar to float" conversion error, although the subquery returns numeric data. It should not fail.select Value from(select cast(HD.Value as float) as Valuefrom HardwareData HDwhere (HD.PropertyName = 'Total Space' and HD.CategoryName = 'Disk Drive(s)')) xwhere x.Value > 9999999 |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-05-05 : 12:46:32
|
| Ahh, it's starting to come back. I think someone else had a similar issue and they never posted how they solved it. I guess this is a good example of why you should store your data in the proper types. :) But, as for a solution, you might need to store the results in a table variable or temp table, then filter those results as needed. |
 |
|
|
|