返回是在月的上半月或下半月返回的函数

时间:2018-07-31 18:53:15

标签: sql-server function date

我正在使用SQL Server学习SQL,但遇到了问题……是否有一种方法可以创建一个函数,该函数在给定特定日期的情况下返回每月的第一天还是下半年。 例如,

如果日期是2018年7月1日,则原因是该月的上半年

如果日期为2018年7月30日,则原因是该月的下半年

我对此没有疑问,我真的开始学习哈哈,对不起! = D

希望我足够清楚^^'

5 个答案:

答案 0 :(得分:1)

您可以在函数中尝试以下类似操作

SELECT CASE
    WHEN DATEPART(d,@Date) <=15 THEN 0
    ELSE 1 END

修改 您的功能看起来像

Create function MyFunc (@myDate datetime)
Returns smallint
as
BEGIN
DECLARE @MidWeek int
IF (DATEPART(m,@myDate)=2)
  SET @MidWeek = 14 -- Special case for Feb (28 or 29 days)
ELSE
  SET @MidWeek = 15 -- Non feb months (30 or 31 days)

IF (DATEPART(d,@myDate) <= @MidWeek )
BEGIN
  RETURN (0)
END

RETURN (1)

END

答案 1 :(得分:1)

我会使用理货标签和NTILE

--this is your input
declare @myDate datetime = '20180413'


--we get the first and last day of that month
declare @startdate datetime = DATEADD(month, DATEDIFF(month, 0, @myDate), 0)
declare @enddate datetime = Dateadd(day,-1,DATEADD(month,1,DATEADD(month, DATEDIFF(month, 0, @myDate), 0)))

--get a tally table of numbers to build our cte of dates for the entire month
;WITH
    E1(N) AS (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)),
    E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
    cteTally(N) AS 
    (
        SELECT  ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E2
    ),

--create the list of dates
dateRange as(
select theDate = @startdate
union
select dateadd(day,N,@startdate)
from cteTally
where dateadd(day,N,@startdate) <= @enddate)


--use NTILE to separate these into 2 groups
select 
    theDate
    ,half_of_month = ntile(2) over (order by theDate)
into #myDates
from dateRange
order by theDate
--this is just to show how ntile works
select * from #myDates


--return the one for your input
select 
    @myDate
    ,half_of_month = case when half_of_month = 1 then 'First' else 'Second' end
from #myDates
where theDate = @myDate

drop table #myDates

Docs on the NTILE function

答案 2 :(得分:0)

这很丑陋,但是它可以为您提供所需的内容,并且不需要查找表。它还可以处理数天的月份。

DECLARE @InputDate datetime; SET @InputDate = GETDATE();

SELECT CASE WHEN @InputDate > DATEADD(DAY, DATEPART(DAY, DATEADD(DAY, -1, DATEADD(MONTH, 1,DATEADD(DAY, ((DATEPART(DAY, @InputDate))*-1)+1, CAST(@InputDate as date))))) / 2, DATEADD(DAY, ((DATEPART(DAY, @InputDate))*-1)+1, CAST(@InputDate as date))) THEN 1 ELSE 0 END AS [MoreThanHalf]

答案 3 :(得分:0)

如果您认为15号是月中

示例

Select sign(DatePart(DAY,'2018-07-04')/15)
      ,sign(DatePart(DAY,'2018-07-16')/15)

答案 4 :(得分:0)

此函数代替创建表格,而是比较当月的当前天和当月的最后一天。如果大于.5,则为每月下半月。

示例是:

  1. 1月15日(15/31)为<0.5,因此是前半段
  2. 2月15日(15/28)为> .5,因此是第二个半月
  3. 4月15日(15/30)= = 5,因此是前半段

    create function PortionOfMonth(@d date)
    returns int
    as
    begin
    declare @returnValue int
    
    if cast(day(@d) as decimal(4,2))  --Convert to decimal for division of ints
        /cast( day(dateadd(d,-1, --last day of current month
                        (dateadd(month,1, --1st day of next month
                                cast(cast(month(@d) as varchar(2)) +'/1/' + cast(year(@d) as varchar(4)) as date))))) --1st day of current month
                                 as decimal(4,2)) --Convert to decimal for division of ints
                                 > .5 
    begin 
        set @returnValue = 1 -- 2nd half
    end
    else 
    begin 
        set @returnValue = 0 --1st half
    end
    
    return @returnValue
    end