SQL substring after second space

I am trying to pull out the all characters after the second space in a column. How do I do this? I don't understand substring and charindex.

DECLARE @a varchar(20)='1 space 2 space 3 space';
SELECT STUFF(@a,1,CHARINDEX(' ',@a,CHARINDEX(' ',@a)+1), '');

Substitute your column/variable/expression where @a appears.

STUFF() replaces a segment of text at a start and end position, in this case, 2 CHARINDEX() functions. CHARINDEX() returns the position where the first text appears in the second.

1 Like

You really should take the time to learn about SUBSTRING and CHARINDEX... they're a part of the fundamentals of the SQL language and you won't get far in life without them,

2 Likes