问题
I have an Access 2013 Database Table, dbo_GOV
THE GOAL
I want to take the USSenators field, which contains data like (below) and split it into USSenator1 and USSenator2 fields, respectively:
John Smith;Sarah Levens
Bill Burr;Kevin Nill
George Thomson;Tracy Johnson
THE PROBLEM
I've tried a few different Access SQL queries... both (below) when executed, give the error message
Invalid use of '.', '!', or '()'. in query expression 'Split(USSenators & ";", ';')(0'.
I have verified that there are 0 records, where USSenators is blank. Every row has 2 people listed, separated by a semicolon.
SQL QUERIES
UPDATE dbo_GOV
SET
USSenator1 = Split(USSenators & ";",';')(0),
USSenator2 = Split(USSenators & ";",';')(1);
UPDATE dbo_GOV
SET
USSenator1 = Split(USSenators,';')(0),
USSenator2 = Split(USSenators,';')(1);
I've tried referencing the Office documentation for Split: here
回答1:
You cannot use Split in a query, use Mid and Instr.
Mid(USSenators,Instr(USSenators,";")+1)
Mid(USSenators,1,Instr(USSenators,";")-1)
Line 2 above returns John Smith for the first record
Line 1 above returns Sarah Levens for the first record
You will need to watch for nulls.
来源:https://stackoverflow.com/questions/22327708/access-db-table-split-field-sql-command