Using PARSENAME to find the last item in a list

I am using Parsename in SQL and would like to extract the last item in the 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 a value of 28, as I expect. However, if I expand my list of more than 4 elements, then the result returns NULL. For instance:

 Declare @string2 as varchar(1000) set @string2 = '25.26.27.28.29' SELECT PARSENAME(@string2, 1) 

I would expect this to return 29, however only NULL is returned

I am sure there is a simple explanation for this, who can help?

+6
source share
2 answers

PARSENAME specifically designed to parse the sql object name. The number of periods in the last example frees him from such a name in order to correctly execute the call.

Instead

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

PARSENAME ( 'object_name' , object_piece )

'object_name' The name of the object for which you want to get the specified part of the object. This name can consist of four parts : server name, database name, owner name, and object name.

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

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

+4
source

Source: https://habr.com/ru/post/908529/


All Articles