我的 SQL 代码中的访问数据类型不匹配

发布于 2024-11-30 17:53:10 字数 445 浏览 0 评论 0原文

我有一个查询,从我的电话系统导入中提取电话分机号和某人的全名,并将全名拆分为名字和姓氏。它对于分割名称非常有用,但如果我尝试按任一名称排序,我会收到数据类型不匹配错误。

这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小女人ら 2024-12-07 17:53:10

我创建了一个 ExportG3R 表,其中包含扩展名名称的文本字段。您的查询使用名称值,例如“Litzner,Mike”。但是,不包含逗号的 name 值(例如“Litzner Mike”)导致我的姓氏出现#Error,而名字出现“Litzner Mike”。如果 name 为 Null,则两者都会出现#Error。直到我尝试按 LastName 和/或 FirstName 进行排序之前,这些都不是 SELECT 的问题。当尝试这些排序时,Access 2003 数据库引擎响应“无效的过程调用”,这与您收到的错误消息不同。

因此,虽然我不确定我的情况与您的情况完全匹配,但我建议您尝试使用 WHERE 子句进行查询,以仅返回具有非 Null name 值并且还包含逗号的行。

SELECT
    e.Extension,
    Left(e.[name],InStr(e.[name],",")-1) AS LastName,
    Trim(Mid(e.[name],InStr(e.[name],",")+1,Len(e.[name])-InStr(e.[name],","))) AS FirstName
FROM ExportG3R AS e
WHERE e.name Like "*,*"
ORDER BY ORDER BY 2,3;

另一个问题是 name 是保留字。当使用 Access 2007 在设计视图中创建该表时,向我发出了有关该字段名称的警告。尝试在设计视图中创建一个一次性表,并在它向您发出有关名称的警告时阅读它提供的帮助。如果可能的话,将其更改为非保留字的内容……也许是全名。

最后,我认为如果将名称字段拆分为表本身的两个字段,这会更简单。将名称存储为“姓、名”在一个字段中组合了 2 个属性。因此,当您需要其中任何一个时,您必须将它们分开。将它们分开存储,然后在需要时将它们连接为“最后,第一个”会更容易:

SELECT LastName & ", " & FirstName AS FullName

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.

SELECT
    e.Extension,
    Left(e.[name],InStr(e.[name],",")-1) AS LastName,
    Trim(Mid(e.[name],InStr(e.[name],",")+1,Len(e.[name])-InStr(e.[name],","))) AS FirstName
FROM ExportG3R AS e
WHERE e.name Like "*,*"
ORDER BY ORDER BY 2,3;

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":

SELECT LastName & ", " & FirstName AS FullName
山田美奈子 2024-12-07 17:53:10

您可以将其包装在子查询中;

SELECT extension,lastname,firstname
FROM (
    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 firstname

you could wrap it in a sub query;

SELECT extension,lastname,firstname
FROM (
    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 firstname
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文