使用 PARSENAME 查找列表中的最后一项
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PARSENAME
专门用于解析 sql 对象名称。后一个示例中的句点数量使其不再是这样的名称,因此调用正确失败。反而
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
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