Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 1 / January 2006

Tip: Looking for answers? Try searching our database.

Convert Text to Int

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
2redline@gmail.com - 11 Jan 2006 21:14 GMT
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--------------------------------------
Steve - 11 Jan 2006 22:12 GMT
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.
Hugo Kornelis - 11 Jan 2006 22:15 GMT
>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
[quoted text clipped - 11 lines]
>((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).

Signature

Hugo Kornelis, SQL Server MVP

2redline - 12 Jan 2006 13:31 GMT
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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.