6
Answers

How to get first word before space or any special character in sql

Pinku

Pinku

Oct 11
390
1

I have 2 condition . i want to get the first word beofre (space or any other special character) and other condition is to get the rest of the word 
let consider string in title like @Title= 'sustainability-energy' or @Title= 'sustainability energy'

DECLARE @TitleDescFirst AS VARCHAR(max)=SUBSTRING(@Title,0,CHARINDEX(' ',@Title,0)) 
DECLARE @TitleDescSecond AS VARCHAR(max)=SUBSTRING(@Title,CHARINDEX(' ',@Title,0) + 1, LEN(@Title)-CHARINDEX(' ',@Title,0)) 

select @TitleDescFirst
Select @TitleDescSecond
I tried above but not getting quite correct result.Please help.It should work for both the condition.

Answers (6)