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. vue项目中 favicon.ico不能正确显示的问题

    方法一:修改index.html文件 <link rel="shortcut icon" type="image/x-icon" href="f ...

  2. vue 自定义全局方法

    import {myfun} from '../static/js/test.js' //se6的正确写法export default {methods:{ diyfun:function () { ...

  3. Ionic4.x 中的button

    官方文档:https://ionicframework.com/docs/api/button 1.ion-button 组件可以定义一个按钮 <ion-button>Default< ...

  4. JSP页面中如何注入Spring容器中的bean

    第一步在JSP页面中导入下面的包: <%@page import="org.springframework.web.context.support.WebApplicationCont ...

  5. 013-多线程-基础-Fork/Join框架、parallelStream讲解

    一.概述 Fork/Join框架是Java7提供了的一个用于并行执行任务的框架, 是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架. 它同ThreadPoolExecut ...

  6. JS微信网页使用图片预览(放大缩小)

    前言 需求在微信网页中客户点击图片可进行预览放大缩小功能,网上找了各种js方式实现, 唯一的麻烦就是不兼容或者和项目框架不兼容 次函数只只用于部分客户端,否则会出现 WeixinJSBridge is ...

  7. Go项目部署到服务器

    -bash: ./main: cannot execute binary file 将 go build main.go 生成的文件上传到服务器后,./main 运行后出新的报错 env GOOS=l ...

  8. JBPM使用

    jbpm+mysql5.7 https://blog.csdn.net/tyn243222791/article/details/79033555

  9. ELK之elasticsearch7版本集群设置

    ELK7版本搭建参考:https://www.cnblogs.com/minseo/p/10948632.html node-1已经安装配置好 配置文件如下 [root@salt-test conf. ...

  10. 【C# 开发技巧】 Application.DoEvents( ) 使用笔记

    该方法可以处理当前队列的消息,比如一个for循环 5000次 向TextBox中追加文本,那肯定会假死一会儿的. 此时便可使用Application.DoEvents()来处理队列的信息. 简单说下使 ...