这段时间的项目用到了sql server,于是一边学习,一边开发;
今天学习了函数的创建,该函数的功能如题所示;
USE [EUC]
GO
/****** Object: UserDefinedFunction [dbo].[get_xun] Script Date: 06/06/2014 16:38:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author>
-- Create date: <Create Date,2014-06-06>
-- Description: <Description,">
-- Parameter desc: @date 输入’yyyy-mm-dd’ ;@startorend 1代表旬开始,其他数字代表旬结束
-- =============================================
CREATE FUNCTION [dbo].[get_xun]
(
@date date,@startorend int
)
RETURNS date
as
BEGIN
declare
@return_date date,
@year int,
@month int,
@day int
set @year=(select YEAR(@date))
set @month=(select MONTH(@date))
set @day=(select DAY(@date))
if @startorend=1
if @day<=10
set @return_date=(select dateadd(dd,-day(@date)+1,@date) )
else if @day<=20
set @return_date=(select dateadd(dd,-day(@date)+11,@date) )
else
set @return_date=(select dateadd(dd,-day(@date)+21,@date) )
else
if @day<=10
set @return_date=(select dateadd(dd,-day(@date)+10,@date) )
else if @day<=20
set @return_date=(select dateadd(dd,-day(@date)+20,@date) )
else
set @return_date=(select dateadd(dd,-1,dateadd(m,1,dateadd(dd,-day(@date)+1,@date))))
RETURN @return_date
END
GO