Microsoft Technologies, ASP.NET & SQL SERVER Tips with Rajat Jaiswal

Parse string in SQL Server

Dear friends,
Recently,
One of my colleagues was working on parsing on string in Sql server. I was thinking of old method like PATINDEX but he suggested a new way which is PARSENAME.

Below is basic syntax of itPARSENAME (‘String with dot value’,index)
Example: suppose we have following string ‘234.456.678.454’
Now if we want to parse it and want 2nd item like 678 then you have to write following statement

DECLARE @StringToParse AS VARCHAR(200)
SET @StringToParse =’234.456.678.454’
SELECT PARSENAME (@StringToParse,2)
When you run above statement then you will get 678.
The mandatory condition here is the string will have only “.” Dot delimiter.
I hope you will also enjoy this new learning.