views:

28

answers:

2

hi, 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'

+1  A: 

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.

Tobiasopdenbrouw
cool. thanks i;ll try it out. :)
marilyn
+2  A: 
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
Martin Smith