Saturday, December 29, 2012

Parse string in Sql Server using PARSENAME()


PARSENAME() function can be used to parse a string. It returns parts from a string delimited by periods.

It works from right to left when parsing the string.

SELECT PARSENAME('AB.CD.EF,GH',1) AS PARSENAME_TEXT Returns EF,GH
SELECT PARSENAME('AB.CD.EF,GH',2) AS PARSENAME_TEXT Returns CD
SELECT PARSENAME('AB.CD.EF,GH',3) AS PARSENAME_TEXT Returns AB
SELECT PARSENAME('AB.CD.EF,GH',4) AS PARSENAME_TEXT Returns NULL

Remember it works with strings which only has 4-parts delimited by period. Can be use full when splitting a IP address.

If you try to use the function with a string with more than 4-parts, it'll return NULL.

No comments:
Write comments
Recommended Posts × +