SELECT * FROM 表 WHERE CONVERT(Nvarchar, dateandtime, 111) = CONVERT(Nvarchar, GETDATE(), 111) ORDER BY dateandtime DESC
2、--统计本周
SELECT * FROM 表 WHERE datediff(week,[dateadd],getdate())=0
3、--统计本月
SELECT * FROM 表 WHERE datediff(month,[dateadd],getdate())=0
4、统计当前
SELECT * FROM 表 WHERE datediff(day,[dateadd],getdate())=0
Select * From table with(nolock) Where Convert(varchar(10),[CreateTime],120) = Convert(varchar(10),getDate(),120)
select count(*) from [user]
where datediff(week,create_day-1,getdate())=0
19、上周注册人数
select count(*) from [user]
where datediff(week,create_day-1,getdate())=1
20、本月注册人数
select count(*) from [user]
where datediff(month,create_day,getdate())=0
21、上月注册人数
select count(*) from [user]
where datediff(month,create_day,getdate())=1
如果要效率,用一下方式
22、查询本周注册人数
select count(*) from [user]
where create_day>=dateadd(day,2-datepart(weekday,getdate()),convert(varchar,getdate(),112))
and create_day<dateadd(day,9-datepart(weekday,getdate()),convert(varchar,getdate(),112))
23、上周注册人数
select count(*) from [user]
where create_day>=dateadd(day,-5-datepart(weekday,getdate()),convert(varchar,getdate(),112))
and create_day<dateadd(day,2-datepart(weekday,getdate()),convert(varchar,getdate(),112))
24、本月注册人数
select count(*) from [user]
where create_day>=dateadd(day,1-day(getdate()),convert(varchar,getdate(),112))
and create_day<dateadd(month,1,dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)))
25、上月注册人数
select count(*) from [user]
where create_day>=dateadd(month,-1,dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)))
and create_day<dateadd(day,1-day(getdate()),convert(varchar,getdate(),112))
26、本周
select count(*) from User
where datediff(dd,create_day,getdate()) <= datepart(dw,getdate())
27、上周
select count(*) from User
where datediff(dd,create_day,(getdate() - datepart(dw,getdate()))) <= 7
28、本月
select count(*) from User
where datepart(mm,create_day) = datepart(mm,getdate())
29、上月
select count(*) from User
where datepart(mm,create_day) = datepart(mm,getdate()) - 1
30、本周注册人数
select count(*) from [User]
where datediff(dd,create_day,getdate()) <= datepart(dw,getdate())
31、上周注册人数
select count(*) from [User]
where datediff(dd,create_day,(getdate() - datepart(dw,getdate()))) <= 7
32、本月注册人数
select count(*) from [User]
where datepart(mm,create_day) = datepart(mm,getdate())
33、上月注册人数
select count(*) from [User]
where datepart(mm,create_day) = datepart(mm,getdate()) - 1
34、查询今日所有
SELECT * from feedback WHERE (DATEDIFF(d,fedtime,GETDATE())=0) ORDER BY fedid DESC
month(create_day)=month(getdate())本月
month(create_day)=month(getdate())-1 上月
今天的所有数据: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 系统函数:
函数 参数/功能 GetDate( ) 返回系统目前的日期与时间 DateDiff (interval,date1,date2) 以interval 指定的方式,返回date2 与date1两个日期之间的差值 date2-date1 DateAdd (interval,number,date) 以interval指定的方式,加上number之后的日期 DatePart (interval,date) 返回日期date中,interval指定部分所对应的整数值 DateName (interval,date) 返回日期date中,interval指定部分所对应的字符串名称
参数 interval的设定值:
值 缩写(Sql Server) Access 和 ASP 说明 Year Yy yyyy 年 1753 ~ 9999 Quarter Qq q 季 1 ~ 4 Month Mm m 月1 ~ 12 Day of year Dy y 一年的日数,一年中的第几日 1-366 Day Dd d 日,1-31 Weekday Dw w 一周的日数,一周中的第几日 1-7 Week Wk ww 周,一年中的第几周 0 ~ 51 Hour Hh h 时0 ~ 23 Minute Mi n 分钟0 ~ 59 Second Ss s 秒 0 ~ 59 Millisecond Ms - 毫秒 0 ~ 999
access 和 asp 中用date()和now()取得系统日期时间;其中DateDiff,DateAdd,DatePart也同是能用于Access和asp中,这些函数的用法也类似
举例:
Select * From VIEW_CountBill Where Month([time]) = Month(getDate())
今天的所有数据: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分钟之前的日期
指定时间 : select * from 表名 where 时间字段 >= to_date('yyyy-MM-dd','1900-01-01');
今天
select * from 表名 where to_days(时间字段名) = to_days(now());
昨天
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1
近7天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)
近30天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)
本月
SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
上一月
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 时间字段名, '%Y%m' ) ) =1
查询本季度数据
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());
查询上季度数据
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
查询本年数据
select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());
查询上年数据
select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));
查询当前这周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());
查询上周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;
查询上个月的数据
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')
select * from user where DATE_FORMAT(pudate,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') ;
select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = WEEKOFYEAR(now())
select * from user where MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now())
select * from user where YEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = YEAR(now()) and MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now())
select * from user where pudate between 上月最后一天 and 下月第一天
查询当前月份的数据
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')
查询距离当前现在6个月的数据
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查询时间区间:
select * from 表 WHERE 时间字段 > '2018-12-11 13:36:31' and 时间字段 <= '2019-01-09 13:36:31'