战地2需要什么配置,山西老陈醋,坏女孩vs酷帅哥
datepart() 函数用于返回日期/时间的单独部分,比如年、月、日、小时、分钟等等。
datediff() 函数返回两个日期之间的时间差。
--查找当天上午的数据 select * from r_attendance where (datepart(hour,rdatetime)>=0 and datepart(hour,rdatetime)<12) and datediff(d,rdatetime,getdate())=0 --查找当天下午的数据 select * from r_attendance where (datepart(hour,rdatetime)>12 and datepart(hour,rdatetime)<=23) and datediff(d,rdatetime,getdate())=0
计算两个时间差
相差年数:select datediff(year,'2017-07-01 11:25:52','2018-07-02 12:25:52'); 结果:1
相差天数:select datediff(day,'2018-07-01 11:25:52','2018-07-02 12:25:52'); 结果:1
相差小时:select datediff(hour,'2018-07-01 11:00:00','2018-07-01 16:00:00'); 结果:5
相差分数:minute,相差秒数:second,相差月数:month
获取当日、月、年的数据:
根据以上计算时间差,我们可以得出结果,如下:
当日:select * from usertable where datediff(day, starttime, getdate())=0
当月:select * from usertable where datediff(month, starttime, getdate())=0
当年:select * from usertable where datediff(year, starttime, getdate())=0
如果想要获取前一天、月或年等,函数只需等于1即可,依次类推即可,反之获取后一个月、年等。函数等于-1,在此就不一一列出了。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
数据库SQL---数据库、基本表、视图、索引的定义、修改、删除
在 Azure CentOS VM 中配置 SQL Server 2019 AG - (上)
在 Azure CentOS VM 中配置 SQL Server 2019 AG - (下)
网友评论