Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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.7k 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

0 votes
1 answer
asked Jul 23, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer
asked Dec 30, 2020 in SQL by Appu (6.1k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...