Forcing a datatype in MS Access make table query
Asked Answered
C

7

5

I have a query in MS Access which creates a table from two subqueries. For two of the columns being created, I'm dividing one column from the first subquery into a column from the second subquery.

The datatype of the first column is a double; the datatype of the second column is decimal, with scale of 2, but I want the second column to be a double as well.

Is there a way to force the datatype when creating a table through a standard make-table Access query?

Coumarone answered 8/12, 2009 at 12:0 Comment(1)
Can you post the SQL statement, please? The original statement will affect the way we answer.Junto
J
8

One way to do it is to explicitly create the table before putting anything into it.

Your current statement is probably like this:

SELECT Persons.LastName,Orders.OrderNo
INTO Persons_Order_Backup
FROM Persons
INNER JOIN Orders
ON Persons.P_Id=Orders.P_Id
WHERE FirstName = 'Alistair'

But you can also do this:

----Create NewTable
CREATE TABLE NewTable(FirstName VARCHAR(100), LastName VARCHAR(100), Total DOUBLE)
----INSERT INTO NewTableusing SELECT
INSERT INTO NewTable(FirstName, LastName, Total)
SELECT FirstName, LastName, 
FROM Person p
INNER JOIN Orders o
ON p.P_Id = o.P_Id
WHERE p.FirstName = 'Alistair'

This way you have total control over the column types. You can always drop the table later if you need to recreate it.

Junto answered 8/12, 2009 at 12:45 Comment(1)
I think that's the only way to do it, other than altering the data types after the MakeTable runs.Ingest
S
3

You can use the cast to FLOAT function CDBL() but, somewhat bizarrely, the Access Database Engine cannot handle the NULL value, so you must handle this yourself e.g.

SELECT first_column, 
       IIF(second_column IS NULL, NULL, CDBL(second_column)) 
          AS second_column_as_float
  INTO Table666
  FROM MyTest;

...but you're going to need to ALTER TABLE to add your keys, constraints, etc. Better to simply CREATE TABLE first then use INSERT INTO..SELECT to populate it.

Sochor answered 8/12, 2009 at 13:19 Comment(0)
P
0

You can use CDbl around the columns.

Padraig answered 8/12, 2009 at 12:5 Comment(12)
Isn't @onedayone correct that CDbl() won't handle Nulls, so you need to handle it, as he's suggested?Ingest
Then you could use NZ, not just iif. So use CDbl(NZ(ColVal,0))Padraig
Nz() is inferior to my IIF(..IS NULL..) construct: it causes a syntax error when used outside of the Access UI. Also, the data typing gets weird because Nz() in SQL code coerces the result to text. More more details, see allenbrowne.com/QueryPerfIssue.html. In this case, though, wrapping it in the CDBL() function 'merely' creates a performance issue.Sochor
@David W. Fenton: if you indeed believe @onedaywhen be to correct, why didn't you up-vote the answer? It's your community duty :)Sochor
From your link : The Nz() function replaces Null with another value (usually a zero for numbers, or a zero-length string for text.) The new value is a Variant data type, and VBA tags it with a subtype: String, Long, Double, Date, or whatever.Padraig
I didn't upvote your answer because I don't consider it the best answer. Even using CDbl doesn't fix all the problems with MakeTable queries, as you still lack full control over text fields and indexing (and just try doing one with an outer join having all Null values in any of the fields on the outer join side and see what you get). The best solution is creating the table ahead of time, and that got my vote.Ingest
@onedaywhen: while it's good to have the caveat about Nz() not working from outside Access, it's really a tangential issue when the question is about doing this from within Access.Ingest
@David W. Fenton: depends what you mean by "within Access". I can't see anything in the question that suggests the OP is using anything but the Access Database Engine.Sochor
@astander: ...and it goes on to say that when used in SQL the engine coerces the result to 'text'. The article concludes that IIF(..IS NULL..) construct is to be preferred over the Nz() function.Sochor
"I have created a query in MS-Access" seems pretty definitive to me -- the OP is using the Access UI to manipulate data. There is no mention of ASP or PHP or any other environment in which this SQL is going to be used. So, your caveats about Nz() are valuable, but entirely tangential to the question being asked, and thus not worthy of my upvote.Ingest
@David W. Fenton: "'I have created a query in MS-Access' seems pretty definitive to me" -- maybe but I get the impression you generally only see what you want to see. Methinks in this case you extrapolate too far. Back in the old days, when Jet was a valid DBMS for a VB application, many devs used MS Access as a 'SQL Management Studio' to create persisted database objects: tables, FKs, VIEWs, etc. So they would have used language such as 'creating queries in MS Access' but using Nz() would have been a mistake.Sochor
@David W. Fenton: Have you read what Allen Browne wrote about Nz() being inferior in SQL code even when the Access UI does apply? I get the impression that Nz() is only really suitable for code using the Access object model e.g. VBA in Forms.Sochor
R
0

An easy way to do this is to create an empty table with the correct field types and then to an Append-To query and Access will automatically convert the data to the destination field.

Reconnoitre answered 13/11, 2014 at 2:0 Comment(0)
A
0

I had a similar situation, but I had a make-table query creating a field with NUMERIC datatype that I wanted to be short text.

What I did (and I got the idea from Stack) is to create the table with the field in question as Short Text, and at the same time build a delete query to scrub the records. I think it's funny that a DELETE query in access doesn't delete the table, just the records in it - I guess you have to use a DROP TABLE function for that, to purge a table...

Then, I converted my make-table query to an APPEND query, which I'd never done before... and I just added the running of the DELETE query to my process.

Thank you, Stack Overflow !

Steve

Arrogance answered 31/1, 2018 at 22:44 Comment(0)
T
0

I add a '& ""' to the field I want to make sure are stored as text, and a ' *1 ' (as in multiplying the amount by 1) to the fields I want to store as numeric.

Seems to do the trick.

Tatyanatau answered 14/9, 2018 at 16:46 Comment(0)
E
0

To get an Access query to create a table with three numeric output fields from input numeric fields, (it kept wanting to make the output fields text fields), had to combine several of the above suggestions. Pre-establish an empty output table with pre-defined output fields as integer, double and double. In the append query itself, multiply the numeric fields by one. It worked. Finally.

Everybody answered 24/9, 2021 at 23:58 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewDroop

© 2022 - 2024 — McMap. All rights reserved.