偶的第一个SQL SERVER存储过程和函数

create database test
go
use test
go
create procedure dbo.sp_formatdate -- 格式化输出日期
as
declare @year varchar(4)
declare @month varchar(2)
declare @day varchar(2)
declare @minute varchar(2)
declare @hour varchar(2)
declare @second varchar(2)
set @year = datepart(yy,getdate())
set @month = datepart(mm,getdate())
set @day = datepart(dd,getdate())
set @hour = datepart(hh,getdate())
set @minute = datepart(mi,getdate())
set @second = datepart(ss,getdate())
select @year + '-' + @month + '-' + @day + ' ' + @hour + ':' + @minute + ':' + @second

GO

================
CREATE  function dbo.DateFormat
(
    @date datetime
)
returns varchar(10)
as
begin
declare @year varchar(4),
@month varchar(2),
@day varchar(2),
@minute varchar(2),
@hour varchar(2),
@second varchar(2),
@result varchar(10)
set @year = datepart(yy,@date)
set @month = datepart(mm,@date)
set @day = datepart(dd,@date)
set @hour = datepart(hh,@date)
set @minute = datepart(mi,@date)
set @second = datepart(ss,@date)
set @result = @year + '-' + @month + '-' + @day + ' ' + @hour + ':' + @minute + ':' + @second
return @result
end

GO

本文出自 “上帝,咱们不见不散!” 博客,转载请与作者联系!

你可能感兴趣的:(sql,数据库,存储过程,函数,server)