SQL查询月、天、周、年(MySql的实例对比)

it2022-05-09  14

 

SQL Server实现

日期部分缩写yearyy, yyyyquarterqq, qmonthmm, mdayofyeardy, ydaydd, dweekwk, wwweekdaydwHourhhminutemi, nsecondss, smillisecondms

 

 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()) 1011/**//*查询今天的数据,time 是表中代表时间的字段*/ 12select * from users where day(time)=day(getdate()) and month(time)=month(getdate()) and year(time)=year(getdate()) 131415/**//*计算那一天是星期一*/ 16SELECT  DATEADD(wk,  DATEDIFF(wk,0,getdate()),  0)   1718/**//*计算那一天是周末*/ 19select dateadd(wk,datediff(wk,0,getdate()),6) 2021/**//*查询本周的数据*/ 22select * from users where DATEPART(wk, time) = DATEPART(wk, GETDATE()) and DATEPART(yy, time) = DATEPART(yy, GETDATE())  232425/**//*查询本日的记录*/ 26select * from users where (DATEDIFF(dd, time, GETDATE()) = 0) 27

转载于:https://www.cnblogs.com/nxxshxf/p/5590155.html


最新回复(0)