Pinku

Pinku

  • 1.1k
  • 308
  • 46.6k

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

Oct 11 2024 9:04 AM

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)