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
LVL 6
aymoon80Asked:
Who is Participating?
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.

aymoon80Author Commented:
The string to test with:

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

Thanks
0
aymoon80Author Commented:
the required output is:
dbo.udf_GetreaterOf(5 , dbo.udf_GetreaterOf(6,7)  )
0
cyberkiwiCommented:
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?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

aymoon80Author Commented:
yes exactelly
0
cyberkiwiCommented:
This should help you start then.
create function myFunction (@s as nvarchar(max))
returns nvarchar(max)
as
begin
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))
	else
		@s
	end)
end

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

Open in new window

0
aymoon80Author 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,
Ayman
0
cyberkiwiCommented:
I left the comments there to help you see how it was built.
Cheers
create function myFunction2 (@s nvarchar(max))
returns nvarchar(max)
AS
BEGIN
declare @open int
declare @open2 int
declare @close int
declare @argument int

select @open = charindex('{', @s)
while @open > 0
begin
	while @open > 0
	begin
		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)
		begin
			--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
		end
		else
			set @open = @open2
	end
select @open = charindex('{', @s)
end
return @s
END
GO
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

0

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
aymoon80Author Commented:
Wonderful and Excellent solution
Thankyou very much for your valuable efforts :)
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.