Reputation: 500
What is the most efficient way to cut off a varchar after the first space i.e. given 'FIRST_STRING SECOND_STRING THIRD_STRING'
, it should return 'FIRST_STRING'
? This will be run on potentially 100s of thousands of rows.
Upvotes: 1
Views: 99
Reputation: 10807
declare @str nvarchar(max) = 'FIRST_STRING SECOND_STRING THIRD_STRING'; select left(@str, charindex(' ', @str) - 1) GO
| (No column name) | | :--------------- | | FIRST_STRING |
dbfiddle here
Upvotes: 1
Reputation: 81990
Notice the +' '
this will trap any single word
Example
Declare @S varchar(max) = 'FIRST_STRING SECOND_STRING THIRD_STRING'
Select left(@S,charindex(' ',@S+' ')-1)
Returns
FIRST_STRING
Upvotes: 5