0 votes
1 view
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.3k 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).

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...