Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Funciones
Funciones
AS
BEGIN
Cuerpo
RETURN Expresión
END
AS RETURNS TABLE
RETURN InstruccionSelect
IF OBJECT_ID ('num_secciones', 'FN') IS NOT NULL
DROP FUNCTION num_secciones;
GO
As
Begin
Return @num
End
Go
-- ejecución
select dbo.num_secciones()
IF OBJECT_ID ('nombre_mes', 'FN') IS NOT NULL
DROP FUNCTION nombre_mes;
GO
create function nombre_mes (@fecha date)
returns varchar(10)
as
begin
declare @nombre varchar(10)
set @nombre= case month(@fecha)
when 1 then 'Enero'
when 2 then 'Febrero'
when 3 then 'Marzo'
when 4 then 'Abril'
when 5 then 'Mayo' select dbo.nombre_mes('12/02/2020')
when 6 then 'Junio'
when 7 then 'Julio'
when 8 then 'Agosto'
when 9 then 'Setiembre'
when 10 then 'Octubre'
when 11 then 'Noviembre'
when 12 then 'Diciembre'
end--case
return @nombre
end;
IF OBJECT_ID ('num_vocales', 'FN') IS NOT NULL
DROP FUNCTION num_vocales;
GO
create function num_vocales (@nombre varchar(20))
returns int
as
begin
declare @n1 int, @letra varchar(1),@i int
set @n1=0
set @i=1
set @letra=substring(@nombre,@i,1)
while(@letra<>'')
begin
if(@letra='a' or @letra='e' or @letra='i' or @letra='o' or @letra='u' )
begin
set @n1=@n1+1
end
set @i=@i+1
set @letra=substring(@nombre,@i,1)
end
return @n1
end
select dbo.num_vocales('alicia')
IF OBJECT_ID ('formato', 'FN') IS NOT NULL
DROP FUNCTION formato;
GO
CREATE FUNCTION formato (@DateValue AS DATETIME )
RETURNS VARCHAR(MAX)
AS
BEGIN
RETURN
DATENAME(DW, @DateValue)+ ', '+
DATENAME(DAY, @DateValue)+ ' '+
DATENAME(MONTH, @DateValue) +', '+
DATENAME(YEAR, @DateValue)
END
select dbo.formato('12/02/2020')
CREATE PROCEDURE suma_digitos
@num int,
@suma int out
AS
declare @d int
set @suma=0
while(@num<>0)
begin
set @d=@num % 10
set @suma=@suma+@d
set @num=@num/10;
end
exec num_alu_sec