Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First two words in the string - sql server

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

like image 706
Zerotoinfinity Avatar asked Nov 30 '22 05:11

Zerotoinfinity


1 Answers

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
like image 171
Kadir Sümerkent Avatar answered Dec 04 '22 09:12

Kadir Sümerkent