Can you post some code and data to reproduce the problem, please? As this is SWITCH()
in SQL code then I think SQL DDL (CREATE TABLE
etc) and DML (INSERT INTO
to add data) would be most appropriate :)
[Picky point: Access Database SQL does not have a 'boolean' data type. It has a YESNO
data type which can be the NULL
value; three-value logic is not Boolean.]
Here's some SQL DML (ANSI-92 Query Mode syntax) to demonstrate how it works as expected for me:
SELECT TYPENAME
(
SWITCH
(
NULL, #2009-01-01 00:00:00#,
FALSE, #2009-06-15 12:00:00#,
TRUE, #2009-12-31 23:59:59#
)
);
Change any of the 'criterion' values and the value is always returned as 'Date' i.e. of type DATETIME
.
UPDATE:
That TYPENAME
function is a great
tool... Access seems to interpret the
entire "column" of the resultset
differently
Indeed. Because a column can only be one data type the results of TYPENAME()
at the row can be misleading. Row values of mixed types must be 'promoted' to a higher data type. As is usual with the Access Database Engine, the process is entirely opaque and the documentation on the subject completely absent, so you just have to suck it and see e.g.
SELECT #2009-01-01 00:00:00# AS row_value,
TYPENAME(#2009-01-01 00:00:00#) AS row_type
FROM Customers
UNION ALL
SELECT 0.5,
TYPENAME(0.5) AS row_type
FROM Customers
returns 'Date' and 'Decimal' respectively but what will the column be? Apparently, the answer is:
SELECT DT1.row_value, TYPENAME(DT1.row_value) AS column_type
FROM (
SELECT DISTINCT #2009-01-01 00:00:00# AS row_value
FROM Customers
UNION ALL
SELECT DISTINCT 0.5
FROM Customers
) AS DT1;
'String'?!
...which of course isn't even a Access Database Engine SQL data type. So TYPENAME()
, annoyingly, uses the name of the 'best fit' VBA type. For example:
SELECT TYPENAME(CBOOL(0));
returns 'Boolean' even though, as discussed above, there is no Boolean data type in Access Database Engine SQL. And
SELECT TYPENAME(my_binary_col)
returns 'String'. Note the same VBA mapping limitation applies to the CAST
functions (yet another annoyance) e.g. there is no 'cast to BINARY
' function and the CDEC()
function remains broken since Jet 4.0 :(