在Sql中对本周日期进行查询

来源:互联网 发布:npm 安装node sass 编辑:程序博客网 时间:2024/05/18 00:41

sql语句也是很重要的

主要是通过DataPart函数的应用取得今天是周几
--申明变量
declare @sunday datetime
declare @saturday datetime
--给变量赋值
select @sunday = cast(convert(char(10),getdate()-DATEPART(dw, getdate())+1,120)+' 00:00:00.000' as datetime)
select @saturday = cast(convert(char(10),getdate()+(7-DATEPART(dw, getdate())),120)+' 23:59:59.998' as datetime)
---查看变量
select @sunday,@saturday
--查看今天的日期
select getdate()
---进行查询
SELECT * FROM sysobjects WHERE  getdate() between @sunday and @saturday
--对今天是否在本周内进行判断
if(getdate() between @sunday and @saturday)  begin  print '今天在本周内' end
else print '今天不在本周内'

原创粉丝点击