Explore Courses Blog Tutorials Interview Questions
0 votes
in SQL by (20.3k points)

Using SQL Server, how do I split a string so I can access item x?

Take a string "Hello John Smith". How can I split the string by space and access the item at index 1 which should return "John"?

1 Answer

0 votes
by (40.4k points)

You can use the PARSENAME function as the SQL server doesn’t have the split function. So apart from UDF, you can use this.

SELECT PARSENAME(REPLACE('Hello John Smith', ' ', '.'), 2)

PARSENAME: It takes the string as the first argument and splits it on the period character. It also takes the number as its 2nd argument, and the number specifies which segment of the string to be returned (It works from back to front).

Related questions

Browse Categories