Sunday, March 11, 2012

Convert Text to Int

I am trying to write a query in access that will pull results that are
in the database in a text field and convert to where I can get a
average including decimals. Any Ideas?

--------Start SQL------------
SELECT AVG(CAST(dbo_sur_response_answer.answer_text AS int)) AS
avg_correct
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)=478));
--------End SQL------------Integers do not have decimal places.

CInt() will convert a number held in a string to an Integer type.
CDouble() will convert a number held in a string to a Double type.
Doubles have decimal places.|||On 11 Jan 2006 13:14:43 -0800, 2redline@.gmail.com wrote:

>I am trying to write a query in access that will pull results that are
>in the database in a text field and convert to where I can get a
>average including decimals. Any Ideas?
>
>--------Start SQL------------
>SELECT AVG(CAST(dbo_sur_response_answer.answer_text AS int)) AS
>avg_correct
>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)=478));
>--------End SQL------------

Hi 2redline,

I doon't know much about Access, but the following should work in SQL
Server. (I assume that using SQL Server is an option, since you
crossposted this to a SQL Server group).

SELECT AVG(CAST(ra.answer_text 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;

I introduced aliasses and changed spacing to make the query more
readable, changed the Access double quotes to standard SQL single quotes
and removed all the unneeded parenthese. The only actual change I made
was CASTing to decimal(5,2) instead of CASTint to int. The average of a
bunch of integers will be integer as well; if you want a result with
decimals, you'll have to use a datatype with decimals (such as
decimal(5,2), which specifies 3 digits to the left and 2 to the right of
the decimal point).

--
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|||2redline wrote:
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 .|||On 12 Jan 2006 05:31:14 -0800, 2redline wrote:

>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

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)

--
Hugo Kornelis, SQL Server MVP

No comments:

Post a Comment