MSSQL获取昨天,本周,本月。。。

特别说明下:以下统计本周数据时,星期天是作为下周的第一天,而不是本周最后一天,因此你把星期天作为本周最后一天时,你需要在getDate()的基础上减一天,如dateadd(‘day‘, -1, getDate())

本周: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

昨天

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

--如果是在表中查詢

--昨天

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 时间字段 >= 时间字段

时间: 2024-08-01 10:44:49

MSSQL获取昨天,本周,本月。。。的相关文章

MYSQL查询今天昨天本周本月等的数据

mysql查询本季度 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT *FROM表名WHERE TO_DAYS( NOW( ) ) – TO_DAYS( 时间字段名) <= 1 7天 SELECT *FROM表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名) 近30天 SELECT *FROM表名 where DATE_SUB(CURDA

获取昨天/今天/本周/下周/本月/下月/去年/今年时间,默认设置时间

/*处理时间戳*/var getTimeStamp=function(dateStr){ var newstr = dateStr.replace(/-/g,'/'); var date = new Date(newstr); var time_str = date.getTime().toString(); return time_str.substr(0, 13);};/*时间戳转换成正常时间*/var timeChange=function(timeStamp) { var newDate

oracle sql语句取得本周本月本年的数据

[sql] --国内从周一到周日 国外是周日到周六  select to_char(sysdate-1,'D') from dual;--取国内的星期几 去掉减一取国外的星期-- [sql] --取本周时间内的数据  select * from table  where DTIME >=trunc(next_day(sysdate-8,1)+1) and DTIME<=trunc(next_day(sysdate-8,1)+7)+1 ;     select * from table  whe

【转】MSSQL获取指定表的列名信息,描述,数据类型,长度

/* --作用:根据特定的表名查询出字段,以及描述,数据类型,长度,精度,是否自增,是否为空等信息 --作者:wonder QQ:37036846 QQ群:.NET顶级精英群 ID:124766907 --时间:2011-03-23 11:25 --描述:创建存储过程 --参数:@tableName 表名 */ CREATE PROC sp_GetListsColumnInfoByTableName( @tableName nvarchar(255)) AS BEGIN SELECT CASE

java中如何获取昨天的当前日期

在java里,获取昨天的当前日期,可以采用calendar来做,也可以采用date来做:如下: 1.采用calendar来做: Calendar cal=Calendar.getInstance(); //System.out.println(Calendar.DATE);//5 cal.add(Calendar.DATE,-1); Date time=cal.getTime(); System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm

linux中用shell获取昨天、明天或多天前的日期

linux中用shell获取昨天.明天或多天前的日期 时间 2015-12-08 09:33:00 BlogJava-专家区 原文 http://www.blogjava.net/xzclog/archive/2015/12/08/428555.html 主题 Shell 原文地址:http://www.itwis.com/html/os/linux/20100202/7360.html linux中用shell获取昨天.明天或多天前的日期: 在Linux中对man date -d 参数说的比较

java 获取昨天日期

java 获取昨天日期  取昨天的日期,本想的截出来日期减一就好了.又一想不对,如果今天是一号怎么办? 现有两个办法 1:Date as = new Date(new Date().getTime()-24*60*60*1000);  SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");  String time = matter1.format(as);  System.out.println(time); 取出

MSSQL 获取数据库字段类型

1 SELECT 2 col.name AS 列名, 3 typ.name as 数据类型, 4 col.max_length AS 占用字节数, 5 col.precision AS 数字长度, 6 col.scale AS 小数位数, 7 col.is_nullable AS 是否允许非空, 8 col.is_identity AS 是否自增, 9 case when exists 10 ( SELECT 1 11 FROM 12 sys.indexes idx 13 join sys.in

java获取昨天今天明天的日期

我刚写完这个代码,就被开除了........  代码双手奉上以备后用: package mydemo; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class ThreeDay { public static void main(String[] args) { getToday();