sql 查询本周本月问题
---查询今日信息
select * from 表名 where datediff(day,时间字段,getdate())=0
其中day可以换成其他时间函数如month等
---求相差天数
select datediff(day,'2004-01-01',getdate())
--1.一个月第一天的
SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
--2.本周的星期一
SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)
select dateadd(wk,datediff(wk,0,getdate()),6)
--3.一年的第一天
SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
--4.季度的第一天
SELECT DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)
--5.当天的半夜
SELECT DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)
--6.上个月的最后一天
SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))
--7.去年的最后一天
SELECT dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))
--8.本月的最后一天
SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))
--9.本年的最后一天
SELECT dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0))
--10.本月的第一个星期一
select DATEADD(wk,
DATEDIFF(wk,0,dateadd(dd,6-datepart(day,getdate()),getdate())), 0)
--查询本周注册人数
select count(*) from [user]
where datediff(week,create_day-1,getdate())=0
--上周注册人数
select count(*) from [user]
where datediff(week,create_day-1,getdate())=1
--本月注册人数
select count(*) from [user]
where datediff(month,create_day,getdate())=0
--上月注册人数
select count(*) from [user]
where datediff(month,create_day,getdate())=1
--如果要效率,这样写查询
--查询本周注册人数
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))
--上周注册人数
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))
--本月注册人数
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)))
--上月注册人数
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))
--本周
select count(*) from User
where datediff(dd,create_day,getdate()) <= datepart(dw,getdate())
--上周
select count(*) from User
where datediff(dd,create_day,(getdate() - datepart(dw,getdate()))) <= 7
--本月
select count(*) from User
where datepart(mm,create_day) = datepart(mm,getdate())
--上月
select count(*) from User
where datepart(mm,create_day) = datepart(mm,getdate()) - 1
--本周
select count(*) from [User]
where datediff(dd,create_day,getdate()) <= datepart(dw,getdate())
--上周
select count(*) from [User]
where datediff(dd,create_day,(getdate() - datepart(dw,getdate()))) <= 7
--本月
select count(*) from [User]
where datepart(mm,create_day) = datepart(mm,getdate())
--上月
select count(*) from [User]
where datepart(mm,create_day) = datepart(mm,getdate()) - 1
学习
month(create_day)=month(getdate())本月
month(create_day)=month(getdate())-1 上月
补充 查询今日所有的
SELECT * from feedback WHERE (DATEDIFF(d,fedtime,GETDATE())=0) ORDER BY fedid DESC
下面的可能好些,加了判断是不是本年的,上面的本周,本月会把每年的都查出来
查询本月的记录
select * from tableName where DATEPART(mm, theDate) = DATEPART(mm, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
查询本周的记录
select * from tableName where DATEPART(wk, theDate) = DATEPART(wk, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
查询本季的记录
select * from tableName wh
- 本文标题: sql 查询本周本月问题
- 文章分类:【Android】
- 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.