zoukankan      html  css  js  c++  java
  • SQL获取本周、本月、本季度的记录的语句

    前提条件:假设表名为:tableName;时间字段名为:theDate

      ①查询本周的记录

      select * from tableName where DATEPART(wk, theDate) = DATEPART(wk, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())

      ②查询本月的记录

      select * from tableName where DATEPART(mm, theDate) = DATEPART(mm, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())

      ③查询本季的记录

      select * from tableName where DATEPART(qq, theDate) = DATEPART(qq, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())

      select datediff(day,dateadd(mm, datediff(mm,'''',getdate()), ''''),dateadd(mm, datediff(mm,'''',getdate()), ''1900-02-01''))

      ④查询本月记录的通用语句

      SELECT * FROM [表名] WHERE (DATEPART(mm, 日期) = @month) AND (DATEPART(yy, 日期) = @year)

    本周:select * from table where datediff(week,C_CALLTIME,getdate())=0     --C_CALLTIME 为日期字段
    本月:select * from table where datediff(Month,C_CALLTIME,getdate())=0   --C_CALLTIME 为日期字段
    本季:select * from table where datediff(qq,C_CALLTIME,getdate())=0 
    前半年1-6,后半年7-12:select * from table where datepart(mm,C_CALLTIME)/7 = datepart(mm,getdate())/7

  • 相关阅读:
    Pydev Console中文提示乱码的问题
    pg_dump命令帮助信息
    Aptana下Django1.6以后的项目模板结构改造
    老外的项目开发在线服务网站资源
    淘宝技术交流笔记
    Python并发编程的几篇文章
    gevent程序员指南
    IOS UITableView拖动排序功能
    Codeforces 300A Array
    一次mysql优化经历
  • 原文地址:https://www.cnblogs.com/BillLei/p/5568915.html
Copyright © 2011-2022 走看看