This should be a simple query (this is a one-table query with no joins), but I'm getting "DataType Mismatch in Query expression."
Here's the deal. Our account numbers are generally 13 digits long. The first three digits constitute the facility number (FAC). All I'm doing is extracting the first three digits of the string - and converting this to an integer. Take for example this account number
5550123456789
The facility number in this case would be 555, and I basically need to convert it to an integer. The following query succeeds (takes about 60 seconds as the table is quite large).
SELECT Account, Fac
From
(
SELECT Account, CInt(Left(account, 3)) as [Fac] from Accounts
WHERE len(Account)=13 And isNumeric(left(Account,3))
) as FacNos
The above should guarantee that the FAC number is extracted as an INTEGER. So I should be able to add a WHERE clause like this:
WHERE Fac > 300
but doing so generates the error. That is, I get the error when I run:
SELECT Account, [Fac]
From
(
SELECT Account, CInt(Left(account, 3)) as [Fac] from Accounts
WHERE len(Account)=13 And isNumeric(left(Account,3))
) as FacNos
WHERE [Fac] > 300
Most likely the root of the problem is some bad account numbers where the first three "digits" are not actually numeric. But if that's the case, shouldn't the first version of the query fail too? As I said, the first version runs fine. It's only when I add the final WHERE clause that the exception throws. Access apparently thinks there is a syntax error here? I'm lost.
Here's the deal. Our account numbers are generally 13 digits long. The first three digits constitute the facility number (FAC). All I'm doing is extracting the first three digits of the string - and converting this to an integer. Take for example this account number
5550123456789
The facility number in this case would be 555, and I basically need to convert it to an integer. The following query succeeds (takes about 60 seconds as the table is quite large).
SELECT Account, Fac
From
(
SELECT Account, CInt(Left(account, 3)) as [Fac] from Accounts
WHERE len(Account)=13 And isNumeric(left(Account,3))
) as FacNos
The above should guarantee that the FAC number is extracted as an INTEGER. So I should be able to add a WHERE clause like this:
WHERE Fac > 300
but doing so generates the error. That is, I get the error when I run:
SELECT Account, [Fac]
From
(
SELECT Account, CInt(Left(account, 3)) as [Fac] from Accounts
WHERE len(Account)=13 And isNumeric(left(Account,3))
) as FacNos
WHERE [Fac] > 300
Most likely the root of the problem is some bad account numbers where the first three "digits" are not actually numeric. But if that's the case, shouldn't the first version of the query fail too? As I said, the first version runs fine. It's only when I add the final WHERE clause that the exception throws. Access apparently thinks there is a syntax error here? I'm lost.