昨天
select * from tb where datediff(day, 时间字段 ,getdate()) = 1
今天
select * from tb where datediff(day, 时间字段 ,getdate()) = 0
本周
select * from tb where datediff(week, 时间字段 ,getdate()) = 0
上周
select * from tb where datediff(week, 时间字段 ,getdate()) = 1
下周
select * from tb where datediff(week, 时间字段 ,getdate()) = -1
 
上月
Select * From TableName Where DateDiff(month, DateTimCol, GetDate()) = 1
本月
Select * From TableName Where DateDiff(month, DateTimCol, GetDate()) = 0
下月
Select * From TableName Where DateDiff(month, GetDate(), DateTimCol ) = -1
本年
Select * From TableName Where DateDiff(year, GetDate(), DateTimCol ) = 0
 
昨天:dateadd(day,-1,getdate())
明天:dateadd(day,1,getdate())
上月:month(dateadd(month, -1, getdate()))
本月:month(getdate())
下月:month(dateadd(month, 1, getdate()))
 
昨天 
Select * From TableName Where DateDiff(dd, DateTimCol, GetDate()) = 1
明天
Select * From TableName Where DateDiff(dd, GetDate(), DateTimCol) = -1
最近七天
Select * From TableName Where DateDiff(dd, DateTimCol, GetDate()) <= 7
随后七天
 
当前年
select  提出日期, datepart(year,getdate()) as  当前年 from 供方资料表
前一年
select  提出日期, datepart(year,getdate())-1 as  当前年 from 供方资料表
后一年
select  提出日期, datepart(year,getdate())+1 as  当前年 from 供方资料表
 

SQL 日期函数
1.   当前系统日期、时间
     select getdate()

2. dateadd   在向指定日期加上一段时间的基础上,返回新的 datetime 值
    例如:向日期加上2天
    select dateadd(day,2,'2004-10-15′)   –返回:2004-10-17 00:00:00.000

3. datediff 返回跨两个指定日期的日期和时间边界数。
    select datediff(day,'2004-09-01′,'2004-09-18′)    –返回:17

4. datepart 返回代表指定日期的指定日期部分的整数。
   SELECT DATEPART(month, '2004-10-15′)   –返回 10

5. datename 返回代表指定日期的指定日期部分的字符串
    SELECT datename(weekday, '2004-10-15′)   –返回:星期五

6. day(), month(),year() –可以与datepart对照一下

select 当前日期=convert(varchar(10),getdate(),120)
,当前时间=convert(varchar(8),getdate(),114)

select datename(dw,'2004-10-15′)

select 本年第多少周=datename(week,'2004-10-15′)
       ,今天是周几=datename(weekday,'2004-10-15′)

举例:
1.GetDate() 用于sql server :select GetDate()

2.DateDiff('s','2005-07-20′,'2005-7-25 22:56:32′)返回值为 514592 秒
DateDiff('d','2005-07-20′,'2005-7-25 22:56:32′)返回值为 5 天

3.DatePart('w','2005-7-25 22:56:32′)返回值为 2 即星期一(周日为1,周六为7)
DatePart('d','2005-7-25 22:56:32′)返回值为 25即25号
DatePart('y','2005-7-25 22:56:32′)返回值为 206即这一年中第206天
DatePart('yyyy','2005-7-25 22:56:32′)返回值为 2005即2005年

【收集】sql查询统计,周,月,年的更多相关文章

  1. sql查询统计,根据新闻类别ID统计,没有数据显示0

    有两张表,新闻信息表MessageInfo和新闻类别表MessageType.表结构如下: 然后需要实现下面这种查询结果: 这个是我面试时遇到的,上面的新闻类型是乱写的.当时没有做出来,然后回来又研究 ...

  2. SQL Server 统计某个月周末的天数

    ---注意:这里统计的周末包括周5,周6,但不包括周日ALTER FUNCTION [dbo].[GetWeekDaysByMonth] ( @Year INT, @Month INT, @Day I ...

  3. MySql查询日周月

    常用计算日期的函数 日 date(日期) = CURDATE() 自然周 YEARWEEK(date_format(日期,'%Y-%m-%d') , 1) = YEARWEEK(now() , 1) ...

  4. sql查询统计

    SELECT TOP 50 (select text from sys.dm_exec_sql_text(sql_handle)) as [SQL], CAST( ((qs.total_elapsed ...

  5. mysql 查询,天,周,月等写法

    1.查询当天的数据 select * from 表名 where TO_DAYS(时间字段)=TO_DAYS(NOW()); 2.查询当周的数据 select * from 表名 where YEAR ...

  6. SQL查询一周内过生日的用户

    SELECT birthday, )) + '-' + )) + '-' + )) AS datetime) AS Nbirthday FROM CRM_Customer WHERE birthday ...

  7. sql两个日期之间的查询统计

    sql查询统计 sql语句: select count(code) as '统计',create_time as '订单时间' from sp_orders where datediff(create ...

  8. 用sql查询当天,一周,一个月的数据

    用sql查询当天,一周,一个月的数据   数据查询,不管在网站还是在系统,都很常见,下文是介绍最常见的以日期查询的语句 select * from ShopOrder where datediff(w ...

  9. sql 日志统计-日、周、月活跃数

    近日网站需求:统计日志表的 日.周.月活跃数.最终研究了出来了,分享给大家看下.  如果有更好的sql语句也可以评论下方. --日活跃量 ), cr.AddTime, )as addtimt,COUN ...

随机推荐

  1. java配置环境变量-及原因

    为什么java要配置环境变量? 那就要从java的编译和解析过程说起 java文件的编译和解析过程 一.一个hellow.java文件,要经历先编译(变成hellow.class),再解析(解析成机器 ...

  2. bootstropt-table 大量字段整体表单上传之时间处理

    js 中用$('#addUserForm').serialize(),//获取表单中所有数据 传送到前台 (controller) $.ajax({ type : "POST", ...

  3. [nodejs] day1-创建服务器

    一.使用匿名函数(新建文件service.js)创建一个服务器: var http = require("http"); //Node.js自带的 http 模块,并且把它赋值给 ...

  4. Webservice_常用

    官网示例: http://cxf.apache.org/docs/writing-a-service-with-spring.html http://cxf.apache.org/docs/jax-r ...

  5. mysql常用语法

    创建表 create table <表名>( <字段名>  类型(长度) not null primary key auto_increment, **主键 name char ...

  6. jenkins coding.net webhook plugin

    开源地址: https://github.com/yuzd/coding.net =========================================================== ...

  7. selenium框架与chrome浏览器的不兼容问题

    在一次偶然的情况下,在chrome上用selenium框架去抓取某个id为XX的页面元素,使用WebDriver的findElement().click()方法进行点击,原来在firefox浏览器运行 ...

  8. MATLAB中的分类器

    MATLAB中的分类器   目前了解到的MATLAB中分类器有:K近邻分类器,随机森林分类器,朴素贝叶斯,集成学习方法,鉴别分析分类器,支持向量机.现将其主要函数使用方法总结如下,更多细节需参考MAT ...

  9. 非名校毕业年薪20W程序员 心得分享

    大家好,我是落落http://blog.csdn.net/robinson1988/ QQ692162374,其实我选择Oracle是一件很神奇的事情,但是现在回想起来又觉得是命中注定.我2006年就 ...

  10. Transport (VMDB) error -44: Message

     关于点击电源按钮的时候出现了这情况Transport (VMDB) error -44: Message.   虚拟机有个服务没开.开始菜单--运行--services.msc 回车   找到VMw ...