MySQL操作

-- 今天  
select fullName,addedTime from t_user where to_days(addedTime) <= to_days(now()); 

-- 昨天  
select fullName,addedTime from t_user where to_days(NOW()) - TO_DAYS(addedTime) <= 1;  

-- 近7天  
select fullName,addedTime from t_user where date_sub(CURDATE(),INTERVAL 7 DAY) <= DATE(addedTime);  

-- 近30天  
SELECT fullName,addedTime FROM t_user where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(addedTime);

-- 本月  
SELECT fullName,addedTime FROM t_user WHERE DATE_FORMAT( addedTime, '%Y%m' ) = DATE_FORMAT( CURDATE() , '%Y%m' );

-- 上一月  
SELECT fullName,addedTime FROM t_user WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( addedTime, '%Y%m' ) ) =1; 

-- 查询本季度数据  
select fullName,addedTime FROM t_user where QUARTER(addedTime)=QUARTER(now()); 

-- 查询上季度数据  
select fullName,addedTime FROM t_user where QUARTER(addedTime)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));  

-- 查询本年数据  
select fullName,addedTime FROM t_user where YEAR(addedTime)=YEAR(NOW());  

-- 查询上年数据  
select fullName,addedTime FROM t_user where year(addedTime)=year(date_sub(now(),interval 1 year));  
-- 查询距离当前现在6个月的数据  
select fullName,addedTime FROM t_user where addedTime between date_sub(now(),interval 6 month) and now();  

-- 查询当前这周的数据  
SELECT fullName,addedTime FROM t_user WHERE YEARWEEK(date_format(addedTime,'%Y-%m-%d')) = YEARWEEK(now());  

-- 查询上周的数据  
SELECT fullName,addedTime FROM t_user WHERE YEARWEEK(date_format(addedTime,'%Y-%m-%d')) = YEARWEEK(now())-1;  

-- 查询上个月的数据   
select fullName,addedTime FROM t_user where date_format(addedTime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m'); 

-- 查询当前月份的数据
select fullName,addedTime FROM t_user where DATE_FORMAT(addedTime,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m');
select fullName,addedTime FROM t_user where date_format(addedTime,'%Y-%m')=date_format(now(),'%Y-%m'); 

-- 查询指定时间段的数据
select fullName,addedTime FROM t_user where addedTime between  '2017-1-1 00:00:00'  and '2018-1-1 00:00:00';   
select fullName,addedTime FROM t_user where addedTime >='2017-1-1 00:00:00'  and addedTime < '2018-1-1 00:00:00'; 
 

sqlserver操作

今天数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=0

昨天数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=1

7天内数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())<=7

30天内数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())<=30

本月数据:select * from 表名 where DateDiff(mm,datetime类型字段,getdate())=0

本年数据:select * from 表名 where DateDiff(yy,datetime类型字段,getdate())=0
 
 
查询今天是今年的第几天: select datepart(dayofyear,getDate())

查询今天是本月的第几天:1. select datepart(dd, getDate()) 
                        2.select day(getDate())
查询本周的星期一日期是多少 (注意:指定日期不能是周日,如果是周日会计算到下周一去。所以如果是周日要减一天) SELECT DATEADD(wk,DATEDIFF(wk,0,getdate()),0)
 
查询昨天日期:select convert(char,dateadd(DD,-1,getdate()),111)  //111是样式号,(100-114)
 
查询本月第一天日期:Select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) as firstday

查询本月最后一天日期:Select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0)) as lastday      //修改-3的值会有相应的变化
 
本月有多少天:select datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast((cast(year(getdate()) as varchar)+'-'+cast(month(getdate()) as varchar)+'-01' ) as datetime ))))
 
求两个时间段相差几天:select datediff(day,'2012/8/1','2012/8/20') as daysum

在指定的日期上±N天:select convert(char,dateadd(dd,1,'2012/8/20'),111) as riqi    //输出2012/8/21
在指定的日期上±N分钟:select dateadd(mi,-15,getdate())  //查询当前时间15分钟之前的日期​

Logo

更多推荐