convert SPs to user defined function

Please help me convert the result of my stored procedures into user defined function.

SP1--------------------------------------------------------------------------
create procedure to_base36(@s numeric, @result varchar(100) OUTPUT)
as
begin
if @s < 36
begin
if @s < 10
set @result = char(@s+48)
else
set @result = char(@s+55)
end
else
begin
declare @res1 varchar(100)
declare @res2 varchar(100)
declare @s1 numeric
declare @s2 numeric
set @s1 = @s/36
set @s2 = @s-floor(@s/36)*36
exec to_base36 @s1, @res1 OUTPUT
exec to_base36 @s2, @res2 OUTPUT
set @result = @res1 + @res2
end
end

----------SP2-----------------------
create procedure shortstamp(@d datetime, @hs numeric, @result varchar(100) OUTPUT)
as
begin
 
declare @res1 varchar(100)
declare @res2 varchar(100)
 
declare @num1 numeric
declare @num2 numeric
 
set @num1 = 466560 + (datepart(year,@d) - 1900) * 512 + datepart(month,@d) * 32 + datepart(day,@d)
exec to_base36 @num1, @res1 OUTPUT
 
set @num2 = datepart(hour,@d) * 524288 + datepart(minute, @d) * 8192 + datepart(second,@d) * 128 + @hs
exec to_base36 @num2 , @res2 OUTPUT
 
set @result = @res1 + @res2
end
--------------------
---------------------
I am calling these procs in my other SQL scripts to genearate @result value

declare @result varchar(100)
declare @hsecs numeric
declare @date datetime

set @hsecs = 1
set @date = getdate()
exec shortstamp @date, @hsecs, @result OUTPUT
-----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------

It would be of great help.
Vasi04Asked:
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.

RichardCorrieCommented:
1)
create Function dbo.Fn_to_base36(@s numeric)
returns varchar(100)
as
begin
     declare @result varchar(100)
if @s < 36
begin
       if @s < 10
              set @result = char(@s+48)
       else
               set @result = char(@s+55)
end
else
begin
declare @res1 varchar(100)
declare @res2 varchar(100)
declare @s1 numeric
declare @s2 numeric
set @s1 = @s/36
set @s2 = @s-floor(@s/36)*36
exec to_base36 @s1, @res1 OUTPUT
exec to_base36 @s2, @res2 OUTPUT
set @result = @res1 + @res2
end
return @Result
end
-----------------------
2)
create function dbo.fn_shortstamp(@d datetime, @hs numeric)
returns  varchar(100)
as
begin
declare  @result varchar(100)
declare @res1 varchar(100)
declare @res2 varchar(100)
 
declare @num1 numeric
declare @num2 numeric
 
set @num1 = 466560 + (datepart(year,@d) - 1900) * 512 + datepart(month,@d) * 32 + datepart(day,@d)
exec to_base36 @num1, @res1 OUTPUT
 
set @num2 = datepart(hour,@d) * 524288 + datepart(minute, @d) * 8192 + datepart(second,@d) * 128 + @hs
exec to_base36 @num2 , @res2 OUTPUT
 
set @result = @res1 + @res2
return @result
end
---------------------------------------------
Use
declare @result varchar(100)
declare @hsecs numeric
declare @date datetime

set @hsecs = 1
set @date = getdate()
select @result =  dbo.fn_shortstamp(@date, @hsecs)

-- note when using/calling a function you must use the Owner qualifier (ie owner.function)

/Richard

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
Vasi04Author Commented:
I am getting these errors:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'Function'.
Server: Msg 137, Level 15, State 1, Line 6
Must declare the variable '@s'.
Server: Msg 137, Level 15, State 1, Line 8
Must declare the variable '@s'.
Server: Msg 137, Level 15, State 1, Line 9
Must declare the variable '@s'.
Server: Msg 137, Level 15, State 1, Line 11
Must declare the variable '@s'.
Server: Msg 137, Level 15, State 1, Line 19
Must declare the variable '@s'.
Server: Msg 137, Level 15, State 1, Line 20
Must declare the variable '@s'.
Server: Msg 178, Level 15, State 1, Line 25
A RETURN statement with a return status can only be used in a stored procedure.
0
HilaireCommented:
User-defined functions are available only from SQL Server 2000 onwards.
ie NOT available with SQL server 7
what's your version ?
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

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.