SQL function to handle a string

Dear Expertise,
I'm developing an application that use a custom interpreter (function) that I develop in SQL Server 2005  translates my custom script keywords into a SQL syntax to be executed dynamically at run-time the case that I'm not able to solve is:
if the sysntex was in passing paramters as e.g. :
if the custom script keyword [GreaterOf]{5 & [GreaterOf]{6,7} }
and the corresponding code of [GreaterOf] was dbo.udf_GetreaterOf($arguments)

This is the string that you will use to test your code:
dbo.udf_GetreaterOf($arguments){5 & dbo.udf_GetreaterOf($arguments){6,7} }

I want to replace the $arguments by the value between {}

I hope it is clear if not please till me to give more explaination

Thanks for your time
Ayman AboualnourSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ayman AboualnourSoftware DeveloperAuthor Commented:
The string to test with:

dbo.udf_GetreaterOf($arguments){5 , dbo.udf_GetreaterOf($arguments){6,7} }

Ayman AboualnourSoftware DeveloperAuthor Commented:
the required output is:
dbo.udf_GetreaterOf(5 , dbo.udf_GetreaterOf(6,7)  )
I don't understand, is the function to be called like this?

dbo.myFunction( 'dbo.udf_GetreaterOf($arguments){5 , dbo.udf_GetreaterOf($arguments){6,7} }' )
and it returns
dbo.udf_GetreaterOf(5 , dbo.udf_GetreaterOf(6,7)  )

The problem is with the nested {{ }} right?
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Ayman AboualnourSoftware DeveloperAuthor Commented:
yes exactelly
This should help you start then.
create function myFunction (@s as nvarchar(max))
returns nvarchar(max)
declare @openbracket int
declare @closebracket int
declare @argument int
set @closebracket = len(reverse(@s)) - charindex('}', reverse(@s)) +1
set @openbracket = charindex('{', @s)
set @argument = patindex('%$arguments%', @s)
return (case
	when @closebracket >0 and @openbracket >0 and @argument > 0 then
		left(@s, @argument-1)
		+dbo.myFunction(substring(@s, @openbracket+1, @closebracket-@openbracket-1))
		+substring(@s, @argument+10, @openbracket-(@argument+10))
		--+substring(@s, @closebracket+1,len(@s))

select dbo.myFunction( 'dbo.udf_GetreaterOf($arguments){5 , dbo.udf_GetreaterOf($arguments){6,7} }' )

Open in new window

Ayman AboualnourSoftware DeveloperAuthor Commented:
Thanks alot, This is pretty good, the only thing that I have is when I tried another input:

'dbo.udf_GetLower($arguments){ dbo.udf_GetGreater($arguments){ 100 , 200 } , 1000 } *  dbo.udf_GetGreater($arguments){ 10 , 20} * 50/100'

It gives:
'dbo.udf_GetLower( dbo.udf_GetGreater( 100 , 200 } , 1000))'

but it should give:
'dbo.udf_GetLower( dbo.udf_GetGreater( 100 , 200 ) , 1000)) * dbo.udf_GetGreater( 10 , 20) * 50/100'

What is wrong?

The equivalent in my script:
'[GetLowerOf]{ [GetGreaterOf]{ 100 & 200 } & 1000 } *  [GetGreaterOf]{ 10 & 20} * 50/100'

Many Thanks,
I left the comments there to help you see how it was built.
create function myFunction2 (@s nvarchar(max))
returns nvarchar(max)
declare @open int
declare @open2 int
declare @close int
declare @argument int

select @open = charindex('{', @s)
while @open > 0
	while @open > 0
		select @open2 = charindex('{', @s, @open+1), @close = charindex('}', @s, @open+1)
		--select @open, @open2, @close, charindex('{', @s, @open+1)
		if (@close > 0) and (@open2 = 0 or @close < @open2)
			--select 'pat', patindex('%'+reverse('$arguments')+'%', reverse(left(@s, @open-1))),
			--	reverse(left(@s, @open-1)),
			--	reverse('$arguments'),
			--	len(reverse(left(@s, @open-1))),
			--	len(reverse(left(@s, @open-1))) - patindex('%'+reverse('$arguments')+'%', reverse(left(@s, @open-1))) - 18
			select @argument = len(reverse(left(@s, @open-1))) - patindex('%'+reverse('$arguments')+'%', reverse(left(@s, @open-1))) - 8
			--select left(@s, @argument-1) -- before argument
			--	, substring(@s, @open+1, @close-@open-1) -- replace arg
			--	, substring(@s, @argument+10, @open-@argument-10) -- between arg and replacement
			--	, substring(@s, @close+1, len(@s)) -- after replacements
			select @s = left(@s, @argument-1) -- before argument
				+ substring(@s, @open+1, @close-@open-1) -- replace arg
				+ substring(@s, @argument+10, @open-@argument-10) -- between arg and replacement
				+ substring(@s, @close+1, len(@s)) -- after replacements
			--select 'arg', @open, @close, @argument, @s
			set @open = 0 -- exit loop
			set @open = @open2
select @open = charindex('{', @s)
return @s
select dbo.myFunction2('dbo.udf_GetLower($arguments){ dbo.udf_GetGreater($arguments){100 , 200}, 1000 } *  dbo.udf_GetGreater($arguments){ 10 , 20} * 50/100')

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ayman AboualnourSoftware DeveloperAuthor Commented:
Wonderful and Excellent solution
Thankyou very much for your valuable efforts :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.