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.
Muhammad Imran AnsariPosted Oct 11, 2024, 2:42 PM
Hi Pinku,
The @DelimiterPos is the variable to calculate the provided string's PATINDEX (searching patterns within strings). You can remove the + 1 from the Amit reply from this line "ISNULL(NULLIF(PATINDEX('%[^a-zA-Z0-9]%', @Title), 0), LEN(@Title)) + 1" or can see the complete code here.
Baibhav KumarPosted Oct 16, 2024, 7:21 AM
To extract the first word before a space or special character and the remaining part of the string, you can use the following approach for both cases:
PinkuPosted Oct 11, 2024, 11:02 AM
Thanks Imran, But what is @Delimeter here.
I have @Title ='sustainability-energy'
created @TitleDescSecond to get the data. what is @DelimiterPos contain here?
Muhammad Imran AnsariPosted Oct 11, 2024, 10:43 AM
Hi Pinku,
As per the requirement, for the second word then you can modify the second query like:
Thank you.
PinkuPosted Oct 11, 2024, 10:36 AM
HI Amit ,
First word is correct but in second string it is showing like "energy" but i need "-energy". How to do this. If you can answer please reply. Thanks
So i need like if @title='sustainability-energy'
@titlefirst='sustainability'
@titlesecond=-energy'
If its like @title='sustainability energy'
@titlefirst='sustainability'
@titlesecond=energy'
If @title like @title='sustainability- :energy'
@titlefirst='sustainability'
@titlesecond=- :energy'
Amit MohantyPosted Oct 11, 2024, 10:30 AM
Try this