SQL报表语句;SQL获取今日、本周、本月数据
2017-08-29 20:28
447 查看
SQL报表语句 SQL获取今日、本周、本月数据
本日:select * from table where datediff(dd,C_CALLTIME,getdate())=0 --C_CALLTIME 为日期字段本周:select * from table where datediff(week,C_CALLTIME,getdate())=0 --C_CALLTIME 为日期字段
本月:select * from table where datediff(Month,C_CALLTIME,getdate())=0 --C_CALLTIME 为日期字段
本季:select * from table where datediff(qq,C_CALLTIME,getdate())=0
前半年1-6,后半年7-12:select * from table where datepart(mm,C_CALLTIME)/7 = datepart(mm,getdate())/7
sql得到当前系统时间得 日期部分
CONVERT(varchar(10),getDate(),120)
求得到"昨天,今天"日期函数的SQL
所属分类:MS-SQL Server 基础类
----------------------------------------------------------------------
Convert(Datetime,GetDate(),2)
GetDate()得到今天日期2007-03-26 16:14:12.187
1.现在我需要得到只是日期部分,时间部分不要,SQL怎么写?
2.求以下日期SQL:
昨天
明天
最近七天
随后七天
上周
本周
下周
上月
本月
下月
请高手帮忙。谢谢
----------------------------------------------------------------------
1.现在我需要得到只是日期部分,时间部分不要,SQL怎么写?
select convert(varchar(10),getdate(),120)
--------------------------------------------------------
--1.
Select Convert(Varchar(10), GetDate(), 120)
Select Convert(Varchar(10), GetDate(), 121)
--------------------------------------------------------
2.求以下日期SQL:
昨天
select convert(varchar(10),getdate() - 1,120)
明天
select convert(varchar(10),getdate() + 1,120)
最近七天
select * from tb where 时间字段 >= convert(varchar(10),getdate() - 7,120)
随后七天
select * from tb where 时间字段 <= convert(varchar(10),getdate() + 7,120) and 时间字段 >= 时间字段
--------------------------------------------------------
convert和dateadd函数结合使用就可以了。
--------------------------------------------------------
用datediff(day,时间列,getdate())
--------------------------------------------------------
上月
select * from tb where month(时间字段) = month(getdate()) - 1
本月
select * from tb where month(时间字段) = month(getdate())
下月
select * from tb where month(时间字段) = month(getdate()) + 1
--------------------------------------------------------
--2
--如果是在表中查詢
--昨天
Select * From TableName Where DateDiff(dd, DateTimCol, GetDate()) = 1
--明天
Select * From TableName Where DateDiff(dd, GetDate(), DateTimCol) = 1
--最近七天
Select * From TableName Where DateDiff(dd, DateTimCol, GetDate()) <= 7
--随后七天
Select * From TableName Where DateDiff(dd, GetDate(), DateTimCol) <= 7
--上周
Select * From TableName Where DateDiff(wk, DateTimCol, GetDate()) = 1
--本周
Select * From TableName Where DateDiff(wk, DateTimCol, GetDate()) = 0
--下周
Select * From TableName Where DateDiff(wk, GetDate(), DateTimCol ) = 1
--上月
Select * From TableName Where DateDiff(mm, DateTimCol, GetDate()) = 1
--本月
Select * From TableName Where DateDiff(mm, DateTimCol, GetDate()) = 0
--下月
Select * From TableName Where DateDiff(mm, GetDate(), DateTimCol ) = 1
--------------------------------------------------------
本周
select * from tb where datediff(week , 时间字段 ,getdate()) = 0
上周
select * from tb where datediff(week , 时间字段 ,getdate()) = 1
下周
select * from tb where datediff(week , 时间字段 ,getdate()) = -1
--------------------------------------------------------
1.现在我需要得到只是日期部分,时间部分不要,SQL怎么写?
select convert(varchar(10),getdate(),120)
2.求以下日期SQL:
昨天
select convert(varchar(10),getdate() - 1,120)
明天
select convert(varchar(10),getdate() + 1,120)
最近七天
select * from tb where 时间字段 >= convert(varchar(10),getdate() - 7,120)
随后七天
select * from tb where 时间字段 <= convert(varchar(10),getdate() + 7,120) and 时间字段 >= 时间字段
上月
select * from tb where month(时间字段) = month(getdate()) - 1
本月
select * from tb where month(时间字段) = month(getdate())
下月
select * from tb where month(时间字段) = month(getdate()) + 1
本周
select * from tb where datediff(week , 时间字段 ,getdate()) = 0
上周
select * from tb where datediff(week , 时间字段 ,getdate()) = 1
下周
select * from tb where datediff(week , 时间字段 ,getdate()) = -1
--------------------------------------------------------
昨天:dateadd(day,-1,getdate())
明天:dateadd(day,1,getdate())
上月:month(dateadd(month, -1, getdate()))
本月:month(getdate())
下月:month(dateadd(month, 1, getdate()))
注:本文转自 http://www.cnblogs.com/chenghu/archive/2013/09/09/3309118.html
相关文章推荐
- SQL报表语句;SQL获取今日、本周、本月数据
- sql语句获取本周、本月数据
- sql语句获取本周、本月数据
- sql语句获取本周、本月、本年数据
- sql语句获取本周、本月数据 SQL 2010-03-25 00:59:34 阅读63 评论0 字号:大中小
- sql语句获取本周、上一周、本月数据
- sql语句获取本周、本月数据
- sql语句获取本周、本月数据
- sql语句获取本周、上一周、本月数据
- sql语句获取今天、昨天、近7天、本周、上周、本月、上月、半年数据
- oracle SQL语句取本周本月本年的数据
- SQL 获取本月,本周,本日,昨日,上月的数据
- sql语句- 查询 本月 本周 等相关数据
- sql 获取本周、本月数据
- sql获取本周内的数据,本月查询
- sql server2008 查询当天,最近三天,本周,本月,最近一个月,本季度的数据的sql语句
- 获取本周,上周,上月的数据,sql语句
- oracle SQL 语句取本周 本月 本年的数据
- Sql Server中查询当天,最近三天,本周,本月,最近一个月,本季度的数据的sql语句
- 本日、本周、本月和按月取数据的SQL语句