zoukankan      html  css  js  c++  java
  • sql Server获取表中今天、昨天、本周、上周、本月、上月等数据

    在做Sql Server开发的时候有时需要获取表中今天、昨天、本周、上周、本月、上月等数据,这时候就需要使用DATEDIFF()函数及GetDate()函数了。
    DATEDIFF ( datepart , startdate , enddate )
    释义:计算时间差
    datepare值:year | quarter | month | week | day | hour | minute | second | millisecond
    startdate:开始日期
    enddate :结束日期
    GetDate()
    释义:获取当前的系统日期

    下面例子中表名为tablename,条件字段名为inputdate
    查询今天

    SELECT * FROM tablename where DATEDIFF(day,inputdate,GETDATE())=0
    查询昨天

    SELECT * FROM tablename where DATEDIFF(day,inputdate,GETDATE())=1
    查询本周

    SELECT * FROM tablename where datediff(week,inputdate,getdate())=0
    查询上周

    SELECT * FROM tablename where datediff(week,inputdate,getdate())=1
    查询本月

    SELECT * FROM tablename where DATEDIFF(month,inputdate,GETDATE())=0
    查询上月

    SELECT * FROM tablename where DATEDIFF(month,inputdate,GETDATE())=1

    喜欢的朋友请帮忙点个赞!!!
  • 相关阅读:
    NYOJ47 过河问题
    CodeForces1165
    LuoGuP3667
    ZROI#958
    ZROI#957
    KMP小结
    LuoGuP2742[模板]二维凸包
    ZROI#999
    ZROI#997
    ZROI#996
  • 原文地址:https://www.cnblogs.com/mchuang/p/5448543.html
Copyright © 2011-2022 走看看