I have inherited another archaic and inefficient system, and this one uses 5 digit item numbers and sometimes a suffix of 1, 2, or 3 letters as an unique identifier.
The following are all examples of valid item numbers in this field. There are over 20 different suffixes used in total.
Item_Num
00001ABC
002ABC
3214
87522ATT
22P
04828P
00421
This makes the field difficult to work with, but it's something that cannot be changed. It's an enterprise wide system and too many entities use the field. For a query I am writing, I need to separate the two parts of Item_Num
into what I am calling Raw_Num
and Suffix
. This is made difficult by the inconsistent formatting though; VAL()
will not grab the "floating" 0s
and LEFT()
has to grab between 2 and 5 characters. Because of those restrictions, I developed the following code
SELECT
Q2.Item_Num,
SWITCH
(
VAL(LEFT(("1" + Q2.Item_Num)),1)=1 , LEFT(VAL(Q2.Item_Number),5),
VAL(LEFT(("1" + Q2.Item_Num)),2)=10 , "0" + LEFT(VAL(Q2.Item_Number),4),
VAL(LEFT(("1" + Q2.Item_Num)),3)=100 , "00" + LEFT(VAL(Q2.Item_Number),3),
VAL(LEFT(("1" + Q2.Item_Num)),4)=1000 , "000" + LEFT(VAL(Q2.Item_Number),2),
VAL(LEFT(("1" + Q2.Item_Num)),5)=10000, "0000" + LEFT(VAL(Q2.Item_Number),1)
)
AS Raw_Numb
FROM
(
[[SUBQUERY]]
)
AS Q2
In an attempt to get the results of
Item_Num Raw_Num
00001ABC 00001
002ABC 002
3214 3214
87522ATT 87522
22P 22
04828P 04828
00421 00421
That however, results in the error The Microsoft Office Access database engine could not find the object ''
. My search for a solution has taken me to this related question, which leads me to believe that somehow I am trying to reference a nameless object. This confusion is doubled when I know I can run
SELECT
Q2.Item_Num
FROM
(
[[SUBQUERY]]
)
AS Q2
And get the output of
Item_Num
00001ABC
002ABC
3214
87522ATT
22P
04828P
00421
Which leaves me with three interrelated questions:
Is this just a result of a poorly executed SWITCH
statement?
Is this because I'm using a SWITCH
statement in a manner that it can't be used in?
If I'm only referencing Q2.Item_Num
in the statement, how can I be searching for the object ''
?
And a slightly off topic question for this Stack: Is there a better way to do what I'm trying to do?