
Signature
Hugo Kornelis, SQL Server MVP
Had one correction to the last line and removed the t from sit to be
si.
Query Analyzer gives an error of
"Server: Msg 529, Level 16, State 2, Line 1
Explicit conversion from data type text to decimal is not allowed."
This is the same error I got during previous attempts also.
It makes me believe it is something to do with the field type. It is
Text, 16, allow nulls
The following working sql will give me each response but no average
.........
SELECT dbo_sur_response_answer.answer_text
FROM (dbo_sur_response_answer INNER JOIN dbo_sur_subitem ON
dbo_sur_response_answer.subitem_id = dbo_sur_subitem.subitem_id) INNER
JOIN dbo_sur_response ON dbo_sur_response_answer.response_id =
dbo_sur_response.response_id
WHERE (((dbo_sur_response.completed_yn)="Y") AND
((dbo_sur_subitem.subitem_id)=[enter SubID]));
Results
-------------------
25.1
20.9
1.12
Any more ideas?
Thanks
Lyle Fairfield - 12 Jan 2006 13:44 GMT
Explicit conversion from data type text to decimal is not allowed."
BOL gives the follwing defintion for text:
Variable-length non-Unicode data in the code page of the server and
with a maximum length of 231-1 (2,147,483,647) characters. When the
server code page uses double-byte characters, the storage is still
2,147,483,647 bytes. Depending on the character string, the storage
size may be less than 2,147,483,647 bytes.
I think of this as analogous in some ways to the JET Memo type.
I am guessing that:
the error message is incorrect and that actually this field is of type
varchar or similar,
or
the creator of this table made some careless error in using a text
field to hold string data that might be converted to numeric data type.
Should my guesses be right, a solution is to change the datatype of the
field (carefully!) or ... I suppose that some legerdemain might be
attempted in the T-SQL to convert the Text to VarChar to Money or
whataver .
Hugo Kornelis - 12 Jan 2006 22:10 GMT
>Had one correction to the last line and removed the t from sit to be
>si.
[quoted text clipped - 7 lines]
>It makes me believe it is something to do with the field type. It is
>Text, 16, allow nulls
Hi 2redline,
Lyle's answer gives you the reason for the error.
You can get around this by first casting from text to varchar, then
casting that to decimal:
SELECT AVG(CAST(CAST(ra.answer_text AS varchar(40)) AS
decimal(5,2))) AS avg_correct
FROM dbo.sur_response_answer AS ra
INNER JOIN dbo.sur_subitem AS si
ON si.subitem_id = ra.subitem_id
INNER JOIN dbo.sur_response AS r
ON r.response_id = ra.response_id
WHERE r.completed_yn = 'Y'
AND sit.subitem_id = 478;
(untested - see www.aspfaq.com/5006 if you prefer a tested reply)

Signature
Hugo Kornelis, SQL Server MVP