Access DB Table - Split Field SQL Command

半世苍凉 提交于 2019-12-31 03:59:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!