SQL 获取日期中第一次出现的 15 号

发布于 2024-11-07 17:24:25 字数 97 浏览 0 评论 0原文

我需要编写一个函数或 SP 来返回 15 号的第一次出现。例如,如果我传递日期为 5 月 8 日,那么它应该返回 5 月 15 日。如果我5月30日过去,那么应该在6月15日返回。

I need to write a function or SP that will return the first occurance of the 15th. For example, if I pass the date as May 8th, then it should return May 15th. If I pass May 30th, then it should return June 15th.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

深海里的那抹蓝 2024-11-14 17:24:25

单程

   DECLARE @d DATETIME
    SELECT @d = '20110508'
    --SELECT @d = '20110530'


    SELECT  CASE WHEN DAY(@d)  > 15 
    THEN  dateadd(mm, datediff(mm, 0, @d)+1, 0) + 14
    ELSE dateadd(mm, datediff(mm, 0, @d)+0, 0)+ 14 end

One way

   DECLARE @d DATETIME
    SELECT @d = '20110508'
    --SELECT @d = '20110530'


    SELECT  CASE WHEN DAY(@d)  > 15 
    THEN  dateadd(mm, datediff(mm, 0, @d)+1, 0) + 14
    ELSE dateadd(mm, datediff(mm, 0, @d)+0, 0)+ 14 end
半边脸i 2024-11-14 17:24:25

怎么样;

create function udf_getNextDate(@base datetime, @day int) returns datetime as begin
    set @base = case when day(@base) > @day         
            then dateadd(month, 1, @base)
        else @base
    end
    return dateadd(day, -day(@base) + @day, @base)
end

select 
  dbo.udf_getNextDate('08 may 2011', 15),
  dbo.udf_getNextDate('30 may 2011', 15),
  dbo.udf_getNextDate('16 dec 2011', 15),
  dbo.udf_getNextDate('01 may 2011', 15)

2011-05-15 00:00:00.000 
2011-06-15 00:00:00.000 
2012-01-15 00:00:00.000 
2011-05-15 00:00:00.000

How about;

create function udf_getNextDate(@base datetime, @day int) returns datetime as begin
    set @base = case when day(@base) > @day         
            then dateadd(month, 1, @base)
        else @base
    end
    return dateadd(day, -day(@base) + @day, @base)
end

select 
  dbo.udf_getNextDate('08 may 2011', 15),
  dbo.udf_getNextDate('30 may 2011', 15),
  dbo.udf_getNextDate('16 dec 2011', 15),
  dbo.udf_getNextDate('01 may 2011', 15)

2011-05-15 00:00:00.000 
2011-06-15 00:00:00.000 
2012-01-15 00:00:00.000 
2011-05-15 00:00:00.000
你丑哭了我 2024-11-14 17:24:25

只是另一种方法:

Declare @d datetime

Set @d = getdate()


Select  Case 
            When    DateDiff(Day, Day(@d), 15) < 0 then 
                    DateAdd(month, 1, DateAdd(Day, DateDiff(Day, Day(@d), 15), @d))
            Else    DateAdd(Day, DateDiff(Day, Day(@d), 15), @d) 
        End as [Next15th]

Just another way of doing it:

Declare @d datetime

Set @d = getdate()


Select  Case 
            When    DateDiff(Day, Day(@d), 15) < 0 then 
                    DateAdd(month, 1, DateAdd(Day, DateDiff(Day, Day(@d), 15), @d))
            Else    DateAdd(Day, DateDiff(Day, Day(@d), 15), @d) 
        End as [Next15th]
孤寂小茶 2024-11-14 17:24:25

这个功能可能对你有帮助

create function Get15th(@date datetime)
returns datetime 
as
begin
declare @resultdate datetime
declare @y int
declare @m int
declare @d int
set @y = datepart(year,@date)
set @m = datepart(month,@date)
set @d = datepart(day,@date)
if( @d<=15)
set @resultdate =cast((str(@y)+'-'+str(@m)+'-15') as datetime)
else
set @resultdate =cast((str(@y)+'-'+str(@m+1)+'-15') as datetime)
return  @resultdate 
end

this function may helps you

create function Get15th(@date datetime)
returns datetime 
as
begin
declare @resultdate datetime
declare @y int
declare @m int
declare @d int
set @y = datepart(year,@date)
set @m = datepart(month,@date)
set @d = datepart(day,@date)
if( @d<=15)
set @resultdate =cast((str(@y)+'-'+str(@m)+'-15') as datetime)
else
set @resultdate =cast((str(@y)+'-'+str(@m+1)+'-15') as datetime)
return  @resultdate 
end
街角迷惘 2024-11-14 17:24:25

尝试

DATEADD(Day, DATEDIFF(Day, 15, Created), 0) AS CreatedDay 

在这里解释 http://improve。 dk/archive/2006/12/13/sql-server-datetime-rounding-made-easy.aspx

使用该链接您可以实现您想要的

更新:弄错了......参见民主党的回答

try

DATEADD(Day, DATEDIFF(Day, 15, Created), 0) AS CreatedDay 

explained here http://improve.dk/archive/2006/12/13/sql-server-datetime-rounding-made-easy.aspx

using that link you can achive what you want

UPDATE: got it wrong ... see Dems answer

祁梦 2024-11-14 17:24:25

Rob 的答案很接近...

如果您以月而不是天为单位获取 datediff,并将您的基数设为一个月的 15 号,然后再添加一个月...

DATEADD(MONTH, DATEDIFF) (MONTH, 14, Created) + 1, 14)

EDIT

修改为使用 DATEDIFF MONTH 的工作方式,而不是我认为的工作方式;)

DATEADD(MONTH, DATEDIFF(MONTH, 0, 创建时间 - 15) + 1, 14)

Rob's answer is close...

if you take the datediff in months, rather than days, and make your base the 15'th of a month, then add one extra month...

DATEADD(MONTH, DATEDIFF(MONTH, 14, Created) + 1, 14)

EDIT

Modified to use DATEDIFF MONTH how it works, not how I thought it should work ;)

DATEADD(MONTH, DATEDIFF(MONTH, 0, Created - 15) + 1, 14)

她如夕阳 2024-11-14 17:24:25

我通常会做这样的事情:

declare
  @date       datetime ,
  @target_day int

set @date       = 'June 16, 2009'
set @target_day = 15

select date      = @date ,
       next_date = case when day(@date) <= @target_day
                     then dateadd(day,15-day(@date),@date)
                     else dateadd(day,15,dateadd(month,1,dateadd(day,-day(@date),@date)))
                   end

处理该月的最后一天有点棘手,因为月份的天数是可变的,并且 SQL Server 的日期数学有时有点巴洛克。

I usually do something like this:

declare
  @date       datetime ,
  @target_day int

set @date       = 'June 16, 2009'
set @target_day = 15

select date      = @date ,
       next_date = case when day(@date) <= @target_day
                     then dateadd(day,15-day(@date),@date)
                     else dateadd(day,15,dateadd(month,1,dateadd(day,-day(@date),@date)))
                   end

dealing with the last day of the month is a wee bit trickier, since month's have variable numbers of days, and SQL Server's date math is sometimes a wee bit baroque.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文