我的 SQL 代码中的访问数据类型不匹配
我有一个查询,从我的电话系统导入中提取电话分机号和某人的全名,并将全名拆分为名字和姓氏。它对于分割名称非常有用,但如果我尝试按任一名称排序,我会收到数据类型不匹配错误。
这是 SQL 代码。
SELECT ExportG3R.Extension,
Left([ExportG3R.name],InStr([ExportG3R.name],",")-1) AS LastName,
Trim(Mid([ExportG3R.name],InStr([ExportG3R.name],",")+1,Len([ExportG3R.name])-InStr([ExportG3R.name],","))) AS FirstName
FROM ExportG3R
ORDER BY ExportG3R.Extension;
关于如何让它发挥作用有什么想法吗?
I have a query that pulls the phone extension and the full name of a person from my phone system import, and splits the full name into a First and Last name. It is working great for spliting the name up but if I try to sort by either name I get a Data Tyep Mismatch Error.
Here is the SQL Code.
SELECT ExportG3R.Extension,
Left([ExportG3R.name],InStr([ExportG3R.name],",")-1) AS LastName,
Trim(Mid([ExportG3R.name],InStr([ExportG3R.name],",")+1,Len([ExportG3R.name])-InStr([ExportG3R.name],","))) AS FirstName
FROM ExportG3R
ORDER BY ExportG3R.Extension;
Any ideas on how to get this working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我创建了一个 ExportG3R 表,其中包含扩展名和名称的文本字段。您的查询使用名称值,例如“Litzner,Mike”。但是,不包含逗号的 name 值(例如“Litzner Mike”)导致我的姓氏出现#Error,而名字出现“Litzner Mike”。如果 name 为 Null,则两者都会出现#Error。直到我尝试按 LastName 和/或 FirstName 进行排序之前,这些都不是 SELECT 的问题。当尝试这些排序时,Access 2003 数据库引擎响应“无效的过程调用”,这与您收到的错误消息不同。
因此,虽然我不确定我的情况与您的情况完全匹配,但我建议您尝试使用 WHERE 子句进行查询,以仅返回具有非 Null name 值并且还包含逗号的行。
另一个问题是 name 是保留字。当使用 Access 2007 在设计视图中创建该表时,向我发出了有关该字段名称的警告。尝试在设计视图中创建一个一次性表,并在它向您发出有关名称的警告时阅读它提供的帮助。如果可能的话,将其更改为非保留字的内容……也许是全名。
最后,我认为如果将名称字段拆分为表本身的两个字段,这会更简单。将名称存储为“姓、名”在一个字段中组合了 2 个属性。因此,当您需要其中任何一个时,您必须将它们分开。将它们分开存储,然后在需要时将它们连接为“最后,第一个”会更容易:
I created a ExportG3R table with text fields for Extension and name. Your query worked with name values like "Litzner, Mike". However, a name value which doesn't include a comma, such as "Litzner Mike", gave me #Error for LastName and "Litzner Mike" for FirstName. And if name is Null, it gives me #Error for both. Neither of those was a deal-breaker for SELECT until I tried to sort on LastName and/or FirstName. When attempting those sorts, the Access 2003 db engine responded "Invalid procedure call", which is not the same error message you're getting.
So, although I'm not certain my situation exactly matches yours, I'll suggest you try your query with a WHERE clause to return only rows which have non-Null name values and also include a comma.
Another concern is that name is a reserved word. When creating that table in design view with Access 2007, gave me a warning about that field name. Try creating a throw-away table in Design View and read the help it offers when it gives you a warning about name. If at all possible, change it to something which isn't a reserved word ... FullName perhaps.
Finally I think this would be simpler if the name field were split into two fields in the table itself. Storing names as "Last, First" combines 2 attributes in one field. So when you need either of them, you must split them out. It's easier to store them separately, then concatenate them whenever you need them as "Last, First":
您可以将其包装在子查询中;
you could wrap it in a sub query;