I have string like this " This is a hello world example"
Now I want first two words of the sentence as my output in SQL Server. i.e. This is .
Another example: Original sentence : "Complete word exercise" Output: Complete word
You can use a query as follows:
DECLARE @d nvarchar(100)
SET @d = 'Complete word exercise'
SELECT SUBSTRING(@d, 0, CHARINDEX(' ', @d, CHARINDEX(' ', @d, 0)+1))
Or alternatively when used in a query:
SELECT SUBSTRING(field1, 0, CHARINDEX(' ', field1, CHARINDEX(' ', field1, 0)+1))
FROM Table
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With