在做查询时,要求:一星期内的信息,一个月内的,半年内和一年内的数据信息,可这些日期,怎么取出来呢?如一星期内的SQL要怎么写?.......
--一星期
where 时间字段 between 时间字段 and DATEADD (week ,1,时间字段)
--一月
where 时间字段 between 时间字段 and DATEADD (month,1,时间字段)
--半年
where 时间字段 between 时间字段 and DATEADD (month,6,时间字段)
--一年
where 时间字段 between 时间字段 and DATEADD (year,1,时间字段)
DATEDIFF
返回跨两个指定日期的日期和时间边界数。
语法
DATEDIFF ( datepart , startdate , enddate )
参数
datepart
是规定了应在日期的哪一部分计算差额的参数。下表列出了 Microsoft® SQL Server™ 识别的日期部分和缩写。
日期部分 缩写
year yy, yyyy
quarter qq, q
Month mm, m
dayofyear dy, y
Day dd, d
Week wk, ww
Hour hh
minute mi, n
second ss, s
millisecond ms