-- =============================================
-- Author: hehai
-- Create date: 2015-11-19 11:01:01
-- Description: 判定时间是否在时间区间范围内。
-- =============================================
CREATE FUNCTION fn_pdsj
(
@date DATETIME, -- 时间
@date_Range_min DATETIME, -- 时间区间(起始值)
@date_Range_max DATETIME -- 时间区间(结束值)
)
RETURNS BIT
AS
BEGIN
DECLARE @return BIT
SELECT @return=1 FROM sys.objects WHERE @date BETWEEN @date_Range_min AND @date_Range_max
IF @return IS NULL SET @return=0
RETURN @return -- @min_seconds>0 AND @max_seconds>0
END
GO
CREATE FUNCTION [dbo].[fn_getyu]
( @datea VARCHAR(10),
@pda VARCHAR(2)--0时开始时间 1时结束时间
)
RETURNS date
AS
BEGIN
declare @daa date
declare @kssj date
declare @jssj date
declare @y VARCHAR(2)
declare @pd VARCHAR(2)
set @y=MONTH(@datea)
DECLARE @date_Range_min DATETIME=cast(YEAR(@datea) as VARCHAR(4))+'-'+cast(month(@datea) as VARCHAR(2))+'-26'
DECLARE @date_Range_max DATETIME=dateadd(d,-day(@datea),dateadd(m,1,@datea))
SELECT @pd= jbcsj.dbo.fn_pdsj(@datea,@date_Range_min,@date_Range_max)
IF @pd=0
BEGIN
IF @y=1
BEGIN
set @kssj=dateadd(dd,-6,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))
END
ELSE
BEGIN
set @kssj=cast(YEAR(@datea) as VARCHAR(4))+'-'+cast(month(dateadd(month, -1, @datea)) as VARCHAR(2))+'-26'
END
set @jssj=cast(YEAR(@datea) as VARCHAR(4))+'-'+cast(month(@datea) as VARCHAR(2))+'-26'
END
ELSE
BEGIN
set @kssj=cast(YEAR(@datea) as VARCHAR(4))+'-'+cast(month(@datea) as VARCHAR(2))+'-26'
set @jssj=cast(YEAR(@datea) as VARCHAR(4))+'-'+cast(month(@datea)+1 as VARCHAR(2))+'-26'
END
IF @pda=0
BEGIN
set @daa=@kssj
END
ELSE
BEGIN
set @daa=@jssj
END
RETURN @daa
END
GO
declare @daa date='2019-11-24'
SELECT @kssj= jbcsj.dbo.fn_getyu(@datea,0)
SELECT @jssj= jbcsj.dbo.fn_getyu(@datea,1)
SELECT @kssj,@jssj