使用 PARSENAME 查找列表中的最后一项

发布于 2025-01-06 15:06:23 字数 421 浏览 2 评论 0原文

我在 SQL 中使用 Parsename 并希望提取项目列表中的最后一个元素。我正在使用以下代码。

Declare @string as varchar(1000)
set @string = '25.26.27.28'

SELECT PARSENAME(@string, 1)

这有效并返回值 28,正如我所期望的那样。但是,如果我将列表扩展至超过 4 个项目,则结果将返回 NULL。例如:

Declare @string2 as varchar(1000)
set @string2 = '25.26.27.28.29'

SELECT PARSENAME(@string2, 1)

我希望返回值 29,但只返回 NULL

我确信对此有一个简单的解释,有人可以帮忙吗?

I am using Parsename in SQL and would like to extract the last element in a list of items. I am using the following code.

Declare @string as varchar(1000)
set @string = '25.26.27.28'

SELECT PARSENAME(@string, 1)

This works and returns the value 28 as I expect. However if I expand my list past more than 4 items then the result returns a NULL. For example:

Declare @string2 as varchar(1000)
set @string2 = '25.26.27.28.29'

SELECT PARSENAME(@string2, 1)

I would expect this to return a value of 29 however only NULL is returned

I'm sure there is a simple explaination to this can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

独夜无伴 2025-01-13 15:06:23

PARSENAME 专门用于解析 sql 对象名称。后一个示例中的句点数量使其不再是这样的名称,因此调用正确失败。

反而

select right(@string2, charindex('.', reverse(@string2), 1) - 1)

PARSENAME is designed specifically to parse an sql object name. The number of periods in the latter example exempt it from being such a name so the call correctly fails.

Instead

select right(@string2, charindex('.', reverse(@string2), 1) - 1)
不知所踪 2025-01-13 15:06:23

PARSENAME ('object_name', object_piece)

'object_name'
是要检索其指定对象部分的对象的名称。
该名称可以由四个部分组成:服务器名称、数据库名称、所有者名称和对象名称。

如果我们给出超过 4 个部分,它将始终返回 null。

参考:http://msdn.microsoft.com/en-us/library/ ms188006.aspx

PARSENAME ( 'object_name' , object_piece )

'object_name'
Is the name of the object for which to retrieve the specified object part.
This name can have four parts: the server name, the database name, the owner name, and the object name.

If we give more than 4 parts, it will always return null.

For Ref: http://msdn.microsoft.com/en-us/library/ms188006.aspx

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