0

How can we explicitly convert data types in INSERT INTO/SELECT queries in MS Access?

Sample Query:

INSERT INTO pStudents( pStudentID, pDate, pRate, pGrade )
SELECT sStudentID, sDate, sRate, sGrade
FROM sStudents
WHERE (((sStudents.sStudentID) Is Not Null);

Here I want to convert fields from sStudents table before inserting in pStudents to following:

pStudentID = text
pDate = Short Date
pRate = Double
pGrade = text

Thanks in advance

2
  • can you post a sample query as wela as the datatypes that you want to convert to/from? Commented Feb 28, 2012 at 20:06
  • @bluefeet sample query with data types updated above Commented Feb 28, 2012 at 20:15

3 Answers 3

5

You can use the built-in conversion functions of Access in the queries:

Select CStr(NumericColumn) from Table

...or as an INSERT query:

Insert Into AnotherTable (StringColumn)
Select CStr(NumericColumn) from Table

Did you mean something like that?


EDIT:

Okay, your sample query with conversions would look like this:

INSERT INTO pStudents( pStudentID, pDate, pRate, pGrade )
SELECT CStr(sStudentID), CDate(sDate), CDbl(sRate), CStr(sGrade)
FROM sStudents
WHERE (((sStudents.sStudentID) Is Not Null);

However, this will only work if the columns contain only data that can actually be converted into the given type.

For example, you can convert a String column to Double with CDbl() - but only if the selected rows contain only values that can actually be converted into Double.
As soon as you select one row with a value that contains something else than numeric values (like 'hello'), the conversion will fail.

On the other hand, do you really need the conversions?
Access can convert a lot by itself. For example, you can insert values from a numeric column into a string column, Access will convert it by itself. You don't have to use CStr() for that.

Sign up to request clarification or add additional context in comments.

8 Comments

"As soon as you select one row with a value that contains something else than numeric values the conversion will fail" -- actually, the whole statement will fail. Also it will fail for nulls (note a null is not a value -- actually, in Access it might be: I have a truly marvellous proof of this, which this comment is too small to contain). Also note that the conversion functions are built into the SQL engine: a simple proof is try try using CDEC() (cast to DECIMAL): it takes a different number of arguments than its VBA equivalent and doesn't work! But good answer ;)
I know that the whole statement will fail...but I assumed that it's clear that the whole statement fails if one conversion in the statement fails :)
@onedaywhen I look forward to 2370 in that case.
@Remou: what is the significance of 2370?
@onedaywhen The expected delay for the proof. "This theorem was first conjectured by Pierre de Fermat in 1637, famously in the margin of a copy of Arithmetica where he claimed he had a proof that was too large to fit in the margin. No successful proof was published until 1995"
|
3

This should work. If you want Date, you can use CDate, however this will make sure the date is a short date (http://www.techonthenet.com/access/functions/date/format.php)

INSERT INTO pStudents( pStudentID, pDate, pRate, pGrade ) 
SELECT CStr(sStudentID), Format(sDate, "Short Date"), CDbl(sRate), CStr(sGrade) 
FROM sStudents WHERE (((sStudents.sStudentID) Is Not Null);

Comments

1

There are many conversion function that are available in Access.

CBool(expression)
CByte(expression)
CCur(expression)
CDate(expression)
CDbl(expression)
CDec(expression)
CInt(expression)
CLng(expression)
CSng(expression)
CStr(expression)
CVar(expression)

http://office.microsoft.com/en-us/access-help/type-conversion-functions-HA001229018.aspx

Then you would use these functions in your INSERT INTO/SELECT query:

INSERT INTO table1 (field)...
SELECT Cstr(fieldValue)...
FROM table2

If you need to convert each of the fields in sStudents, then based on your edit you could do the following:

INSERT INTO pStudents( pStudentID, pDate, pRate, pGrade )
SELECT CStr(sStudentID), CDate(sDate), CDbl(sRate), CStr(sGrade)
FROM sStudents
WHERE (((sStudents.sStudentID) Is Not Null);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.