2

I have a problem with a project with an sql function:

DECLARE @Var1 varchar(8000) = '{strong} this is strong{/strong} this is not strong.';

DECLARE @Var2 varchar(8000)

If I want to move the string between '{strong}' and '{/strong}' to @Var2 to have this result

@Var1 = 'this is not strong.' 
@Var2 = '{strong} this is strong {/strong}' 

How should I do it??

PS: I'm still new to sql :(

Thank you

7
  • Database? (I suspect MS SQL Server, but still) Commented Jan 31, 2013 at 9:54
  • Why did you use the SQL tag? Commented Jan 31, 2013 at 9:56
  • no strong is just a word a string nothing specific and yes MS SQL DB Commented Jan 31, 2013 at 9:58
  • If you define the variable yourself why don't you split it up from the beginning? Commented Jan 31, 2013 at 10:02
  • @fragmentedreality - It might be a function parameter in reality. I added the DECLARE to make things easier to follow. Commented Jan 31, 2013 at 10:03

3 Answers 3

2

Try the below Query... it will help you....

declare @var1 varchar(8000)
declare @var2 varchar(8000)
set @var1 ='{strong} this is strong{/strong} this is not strong.'
select @var2 = substring(@var1,charindex('{strong}',@var1)+len('{strong}'),charindex('{/strong}',@var1)-charindex('{strong}',@var1)-len('{strong}'))
SET @var1 = REPLACE(@var1,'{strong}' + @var2 + '{/strong}','')
select @var1 as Var1,@var2 as Var2
Sign up to request clarification or add additional context in comments.

Comments

0

Sql is not the best choice of place to do something like this

If I had to, like someone had a gun pointed at my kids

I'd do something like this, might need adjusting for your chose flavour of sql

This is T-SQL

Declare v1 VarChar(255)
Declare v2 VarChar(255)


set v1 = '{Strong}$REPLACE${/Strong}'
set v2 = 'This is strong'

Select Replace(v1,'$REPLACE$',v2)

The other way you would have to do something like use CharIndex to find '}{/' Then use Substring to chop up V1 (as '{Strong}{/Strong}') and then concatenate them with v2

Comments

0

Using LEFT, RIGHT and CHARINDEX as;

declare @start varchar(50)='{strong}', @end varchar(50)='{/strong}' 

select @var2 = left(@var1,charindex(@end,@var1,1)+len(@end)), 
       @var1= right(@var1,len(@var1)-(charindex(@end,@var1,1)+len(@end)))

SQL DEMO 1

This is bit crazy but generic and works with some text before '{strong}' tag as well.

declare 
@var1 varchar(100)= 'front text{strong} this is strong{/strong} this is not strong', 
@var2 varchar(100)= ''

declare @start varchar(50)='{strong}', @end varchar(50)='{/strong}' 

select @var2 = substring(myString,ci1,ci2+len(@end)-ci1), 
       @var1 = right(myString,len(myString) - (ci2+ len(@end)))
from (
    select @var1 myString, charindex(@start,@var1,1) ci1, 
           charindex(@end,@var1,charindex(@start,@var1,1)) ci2
) T  

select @var2 var2 ,@var1 var1

SQL DEMO 2

--Results
VAR2                                VAR1
{strong} this is strong{/strong}    this is not strong

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.