Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SP_LPAD

Author  Topic 

shifis
Posting Yak Master

157 Posts

Posted - 2006-03-29 : 20:19:05
I did an sp, that do the rpad function, but I want to used has I used a Oracle function in the select statement


select SP_RPAD empname,20,'w' as nombre from tbej

and it sent me an error:

Invalid column name 'SP_RPAD'.

How can I do cretate a function that I can call from a select statement?


--Hace la funcion del RPAD, rellena a la derecha con el caracter que le
-- mandemos hasta que la cadena tenga la longitud que enviamos
--@Cadena
--@Char_llenar
--@LongCadena
CREATE PROCEDURE SP_RPAD
@Cadena varchar(200), --Cadena Original a la que se va a rellenar
@LongCadena int, --Longuitud final que tendra la cadea
@Char_llenar char(1) --Caracter con el que se va a rellenar
AS

SELECT RIGHT(REPLICATE(@Char_llenar,@LongCadena) + @Cadena ,@LongCadena) AS RPAD
GO

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-03-29 : 20:25:59
SP_RPAD is a stored procedure.
You execute the stored procedure this way

exec SP_RPAD 'empname', '20', 'w'


Looking at your select statement
quote:
select SP_RPAD empname,20,'w' as nombre from tbej

you should be creating a function instead.

CREATE FUNCTION F_RPAD
(
@Cadena varchar(200), --Cadena Original a la que se va a rellenar
@LongCadena int, --Longuitud final que tendra la cadea
@Char_llenar char(1) --Caracter con el que se va a rellenar
)
RETURNS VARCHAR(200)
AS
BEGIN
RETURN (RIGHT(REPLICATE(@Char_llenar,@LongCadena) + @Cadena ,@LongCadena))
END

and then you can call your function like

SELECT dbo.F_RPAD(empname, 20, 'w') as nombre
FROM tbej




KH

Choice is an illusion, created between those with power, and those without.
Concordantly, while your first question may be the most pertinent, you may or may not realize it is also the most irrelevant

Go to Top of Page

shifis
Posting Yak Master

157 Posts

Posted - 2006-03-29 : 21:26:42
Thanks!!!
That's what I was looking for!!
Go to Top of Page
   

- Advertisement -