SQL Server实现

日期部分 缩写
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw
Hour hh
minute mi, n
second ss, s
millisecond ms
 1  2/**//*计算今天是星期几*/  3select datename(weekday,getdate())   4  5/**//*查询本年的数据*/  6select * from  users where year(time)=year(getdate())     7  8/**//*查询本月的数据,time是表users中代表时间的字段*/  9select * from users where month(time)=month(getdate()) and year(time)=year(getdate()) 10 11/**//*查询今天的数据,time 是表中代表时间的字段*/ 12select * from users where day(time)=day(getdate()) and month(time)=month(getdate()) and year(time)=year(getdate()) 13 14 15/**//*计算那一天是星期一*/ 16SELECT  DATEADD(wk,  DATEDIFF(wk,0,getdate()),  0)   17 18/**//*计算那一天是周末*/ 19select dateadd(wk,datediff(wk,0,getdate()),6) 20 21/**//*查询本周的数据*/ 22select * from users where DATEPART(wk, time) = DATEPART(wk, GETDATE()) and DATEPART(yy, time) = DATEPART(yy, GETDATE())  23 24 25/**//*查询本日的记录*/ 26select * from users where (DATEDIFF(dd, time, GETDATE()) = 0) 27 28/**//*查询本月的记录*/ 29select * from users where (DATEDIFF(mm, time, GETDATE()) = 0) 30 31/**//*查询本年的记录*/ 32select * from users where (DATEDIFF(yy, time, GETDATE()) = 0)

在MySql中实现:

1——   
 2  本年:   
 3  select   *   from   loanInfo   where   year(date)=year(getdate())   
 4    
 5  2——   
 6  本月:   
 7  select   *   from   loanInfo   where   year(date)=year(getDate())   And   month(date)=month(getdate())   
 8    
 9  3——   
10  本日:   
11  select   *   from   loanInfo   where   year(date)=year(getDate())   And   month(date)=month(getdate())   and   Day(date)=Day(getDate())  
12
13
14
15SELECT   *    FROM   table    WHERE   (MONTH(字段)   =   MONTH(GETDATE()))   

SQL查询月、天、周、年(MySql的实例对比)的更多相关文章

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

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

  2. Thinkphp中查询复杂sql查询表达式,如何表达MYSQL中的某字段不为空is not null?

    Thinkphp中查询复杂sql查询表达式,如何表达MYSQL中的某字段不为空is not null?先上两种实现方式的实例:$querys["house_type_image"] ...

  3. sql查询当天,一周,一个月数据的语句

    --查询当天:  select * from info where DateDiff(dd,datetime,getdate())=0 --查询24小时内的: select * from info w ...

  4. 【收集】sql查询统计,周,月,年

    昨天 select * from tb where datediff(day, 时间字段 ,getdate()) = 1 今天 select * from tb where datediff(day, ...

  5. sql查询月的数据

    https://zhidao.baidu.com/question/557935059.html

  6. Mysql 查询天、周,月,季度、年的数据

    Mysql 查询天.周,月,季度.年的数据 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 ...

  7. MySQL GROUP_CONCAT函数使用示例:如何用一个SQL查询出一个班级各个学科第N名是谁?

    如何用一个SQL查询出一个班级各个学科第N名是谁? 首先贴出建表语句,方便大家本地测试: -- 建表语句 CREATE TABLE score ( id INT NOT NULL auto_incre ...

  8. mysql经纬度查询并且计算2KM范围内附近用户的sql查询性能优化实例教程

    之前很傻很天真地以为无非就是逐个计算距离,然后比较出来就行了,然后当碰到访问用户很多,而且数据库中经纬度信息很多的时候,计算量的迅速增长,能让服务器完全傻逼掉,还是老前辈的经验比我们丰富,给了我很大的 ...

  9. Mysql sql查询性能侦查

    Mysql 服务性能优化配置:http://5434718.blog.51cto.com/5424718/1207526[该文章很好] Sql查询性能优化 对Sql进行优化,肯定是该Sql运行未能达到 ...

随机推荐

  1. python小白之矩阵matrix笔记(updating)

    Matrix #python学习之矩阵matrix 2018.4.18 # -*- coding: UTF-8 -*- from numpy import * import numpy as np i ...

  2. R-CNN论文学习

    Rich feature hierarchies for accurate object detection and semantic segmentation Tech report (v5) pr ...

  3. 教你如何在linux上装逼,shell中颜色的设置

    linux启动后环境变量加载的顺序为:etc/profile → /etc/profile.d/*.sh → ~/.bash_profile → ~/.bashrc → [/etc/bashrc] 想 ...

  4. python reduce和偏函数partial

    functools模块 reduce方法: reduce方法 reduce方法,顾名思义就是减少 可迭代对象不能为空,初始值没提供就在可迭代对象中去一个元素 from functools import ...

  5. Flutter布局--appbar导航栏和状态栏

    MaterialApp 先看下上图的具体用法:1. title:标题2. actions:表示右侧的按钮的动作3. leading:表示左侧的按钮的动作4. flexibleSpace:5. back ...

  6. 【ARM-Linux开发】TI 关于Gstreamer使用的几个参考

    http://processors.wiki.ti.com/index.php/Example_GStreamer_Pipelines#H.264_RTP_Streaming http://proce ...

  7. Supported orientations has no common orientation with the application, and [UIAlertController shouldAutorotate] is returning YES

    某一个页面横屏时会出现崩溃的问题描述,原因是当你在AppDelegate中返回的设备方向是UIInterfaceOrientationMaskLandscapeLeft.但是你在视图控制器中返回支持自 ...

  8. EMR的fair-scheduler.xml

    cat /etc/ecm/hadoop-conf/fair-scheduler.xml <?xml version="1.0"?> <allocations> ...

  9. Jmeter plugins 之 Perfmon Metrics Collector(服务器性能监控)

    客户端(Jmeter端) 1.安装plugins manager,然后安装  2.添加listener-(第1步成功后才可看到此功能)  服务端:(要监控的服务器) 1.下载ServerAgent,并 ...

  10. 【坑】springMvc 信息校验,读取不到错误配置信息的问题

    文章目录 前言 ResourceBundleMessageSource 后记 前言 springMvc 的一大利器,validation 检验,通过注解,可以帮我们完成校验,很是顺手. 终极偷懒检验, ...