• sql 指定时间 所在的周、月、季、年



    DECLARE @TodayDateTime DATETIME
    DECLARE @strToday NVARCHAR(19)

    DECLARE @TodayBeginDateTime DATETIME
    DECLARE @TodayEndDateTime DATETIME

    DECLARE @WeekBeginDateTime DATETIME
    DECLARE @WeekEndDateTime DATETIME

    DECLARE @MonthBeginDateTime DATETIME
    DECLARE @MonthEndDateTime DATETIME

    DECLARE @YearBeginDateTime DATETIME
    DECLARE @YearEndDateTime DATETIME

    DECLARE @QuarterBeginDateTime DATETIME
    DECLARE @QuarterEndDateTime DATETIME

    DECLARE @YesterdayBeginDateTime DATETIME
    DECLARE @YesterdayEndDateTime DATETIME

    DECLARE @Day3BeforeBeginDateTime DATETIME
    DECLARE @Day3BeforeEndDateTime DATETIME

    DECLARE @Day7BeforeBeginDateTime DATETIME
    DECLARE @Day7BeforeEndDateTime DATETIME

    DECLARE @Day14BeforeBeginDateTime DATETIME
    DECLARE @Day14BeforeEndDateTime DATETIME

    DECLARE @Day30BeforeBeginDateTime DATETIME
    DECLARE @Day30BeforeEndDateTime DATETIME

    DECLARE @LastMonthBeginDateTime DATETIME
    DECLARE @LastMonthEndDateTime DATETIME

    DECLARE @LastWeekBeginDateTime DATETIME
    DECLARE @LastWeekEndDateTime DATETIME


    DECLARE @DateID INT


    --指定统计时间
    SET @TodayDateTime=GETDATE()-1 --CAST('2011-03-23 00:00:00' AS DATETIME)
    SET @strToday= CONVERT(NVARCHAR(19),GETDATE(),120)
    --当日
    SET @TodayBeginDateTime=DATEADD(dd, DATEDIFF(dd,0,@TodayDateTime),0)
    SET @TodayEndDateTime=DATEADD(ms, -3, DATEADD(dd, DATEDIFF(dd,0,@TodayDateTime),1))
    --本月的第一天和最后一天
    SET @MonthBeginDateTime=DATEADD(mm, DATEDIFF(mm,0,@TodayDateTime), 0)
    SET @MonthEndDateTime=DATEADD(ms, -3, DATEADD(mm, DATEDIFF(m, 0, @TodayDateTime)+1, 0))
    --本年的第一天和最后一天
    SET @YearBeginDateTime=DATEADD(yy, DATEDIFF(yy,0,@TodayDateTime), 0)
    SET @YearEndDateTime=dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,@TodayDateTime)+1, 0))

    --本周的第一天和最后一天(第一天是星期一 。因为系统默认的星期日是一周的第一天,所以计算时应该在 指定的时间上减一天)
    SET @WeekBeginDateTime = DATEADD(wk, DATEDIFF(wk,0,@TodayDateTime), 0)
    SET @WeekEndDateTime = DATEADD(ms, -3, DATEADD(wk, DATEDIFF(wk,0, @TodayDateTime), 7))



    --本季的度第一天和最后一天
    SET @QuarterBeginDateTime=DATEADD(qq, DATEDIFF(qq, 0, @TodayDateTime), 0)
    SET @QuarterEndDateTime= DATEADD(ms, -3, DATEADD(qq, DATEDIFF(qq, 0, @TodayDateTime)+1, 0))
    --昨天
    SET @YesterdayBeginDateTime=DATEADD(dd, DATEDIFF(dd,0,@TodayDateTime),-1)
    SET @YesterdayEndDateTime=DATEADD(ms, -3, DATEADD(dd, DATEDIFF(dd,0,@TodayDateTime),0))
    --三天前
    SET @Day3BeforeBeginDateTime=DATEADD(dd, DATEDIFF(dd,0,@TodayDateTime),-3)
    SET @Day3BeforeEndDateTime=DATEADD(ms, -3, DATEADD(dd, DATEDIFF(dd,0,@TodayDateTime),-2))
    -- 七天前
    SET @Day7BeforeBeginDateTime=DATEADD(dd, DATEDIFF(dd,0,@TodayDateTime),-7)
    SET @Day7BeforeEndDateTime=DATEADD(ms, -3, DATEADD(dd, DATEDIFF(dd,0,@TodayDateTime),-6))
    -- 14天前
    SET @Day14BeforeBeginDateTime=DATEADD(dd, DATEDIFF(dd,0,@TodayDateTime),-14)
    SET @Day14BeforeEndDateTime=DATEADD(ms, -3, DATEADD(dd, DATEDIFF(dd,0,@TodayDateTime),-13))
    -- 30天前
    SET @Day30BeforeBeginDateTime=DATEADD(dd, DATEDIFF(dd,0,@TodayDateTime),-30)
    SET @Day30BeforeEndDateTime=DATEADD(ms, -3, DATEADD(dd, DATEDIFF(dd,0,@TodayDateTime),-29))

    --上个月的第一天和最后一天
    SET @LastMonthBeginDateTime= DATEADD(mm, DATEDIFF(mm, 0, DATEADD(ms,-3,DATEADD(mm, DATEDIFF(mm,0,@TodayDateTime), 0))), 0)
    SET @LastMonthEndDateTime= DATEADD(ms,-3,DATEADD(mm, DATEDIFF(mm,0,@TodayDateTime), 0))
    --上周第一天和最后一天(第一天是星期一)
    SET @LastWeekBeginDateTime = DATEADD(dd, -1, DATEADD(wk,DATEDIFF(wk, 6, @TodayDateTime), 1))
    SET @LastWeekEndDateTime = DATEADD(ms, -3, DATEADD(dd,0,DATEADD(wk,DATEDIFF(wk,6,@TodayDateTime),7)))

    SET @DateID=CAST(CONVERT(CHAR(10), @TodayDateTime, 112) AS INT)


    /*--核对统计时间 */
    SELECT @TodayDateTime AS TodayDateTime
    , @TodayBeginDateTime AS TodayBeginDateTime, @TodayEndDateTime AS TodayEndDateTime
    , @MonthBeginDateTime AS MonthBeginDateTime, @MonthEndDateTime AS MonthEndDateTime
    , @YearBeginDateTime AS YearBeginDateTime, @YearEndDateTime AS YearEndDateTime
    , @WeekBeginDateTime AS WeekBeginDateTime, @WeekEndDateTime AS WeekEndDateTime
    , @QuarterBeginDateTime AS QuarterBeginDateTime, @QuarterEndDateTime AS QuarterEndDateTime
    , @YesterdayBeginDateTime AS YesterdayBeginDateTime, @YesterdayEndDateTime AS YesterdayEndDateTime
    , @Day3BeforeBeginDateTime AS Day3BeforeBeginDateTime, @Day3BeforeEndDateTime AS Day3BeforeEndDateTime
    , @Day7BeforeBeginDateTime AS Day7BeforeBeginDateTime, @Day7BeforeEndDateTime AS Day7BeforeEndDateTime
    , @Day14BeforeBeginDateTime AS Day14BeforeBeginDateTime, @Day14BeforeEndDateTime AS Day14BeforeEndDateTime
    , @Day30BeforeBeginDateTime AS Day30BeforeBeginDateTime, @Day30BeforeEndDateTime AS Day30BeforeEndDateTime
    , @LastMonthBeginDateTime AS LastMonthBeginDateTime, @LastMonthEndDateTime AS LastMonthEndDateTime
    , @LastWeekBeginDateTime AS LastWeekBeginDateTime, @LastWeekEndDateTime AS LastWeekEndDateTime
    , @DateID AS DateID

  • 相关阅读:
    EL表达式(Expression Language)
    JSP简单功能介绍
    MySQL基础使用
    JDBC
    MySQL安装卸载
    stanfordnlp dependencies(依存关系简写表)
    不需要深度学习就能做的文本分类器
    词向量的cbow模型
    pytorch实现自己的textCNN
    OpenCV编译viz模块
  • 原文地址:https://www.cnblogs.com/xiaonanmu/p/5749246.html
Copyright © 2020-2023  润新知