marilyn
marilyn

Reputation: 555

Extracting nth word onwards

how do i extract words from the nth word onwards in sql server?

eg.

| Description |
| This is a nice dress |

extracting the 4th word onwards, would output 'nice dress'

Upvotes: 2

Views: 1883

Answers (2)

Martin Smith
Martin Smith

Reputation: 453067

with sentences as
(
select 'short sentence' as sentence UNION ALL
select 'This is a nice dress' as sentence UNION ALL
select 'The quick brown fox jumped over the lazy dog' as sentence 
)

SELECT 
SUBSTRING(sentence,
CHARINDEX(' ', sentence,CHARINDEX(' ', sentence, CHARINDEX(' ', sentence)+1)+1),
LEN(sentence)) AS WordFourOnwards
FROM sentences
WHERE patindex('[^ ]% [^ ]% [^ ]% [^ ]%',sentence) > 0

Upvotes: 4

Tobiasopdenbrouw
Tobiasopdenbrouw

Reputation: 14039

If you build the method yourself, you could find the string position for the third space, and then take the right string from that position.

Edit: combination of charindex() and substring(), etc.

Upvotes: 1

Related Questions