关于SqlServer中datediff用法
datediff是SQL SERVER里面的用法,ORACLE没有
[var]
[var]
是后面日期减去前面的日期(enddate-startdate)
1、datepart,规定应在日期的哪一部分计算差额
日期 缩写
year yy/yyyy
quarter qq/q
Month mm/m
day dd/d
Hour hh/h
minute mi,n
second ss,s
[var]
SQL SERVER不用像ORACLE一样写 select … from dual,只需要写select ….就可以了
1、参数是 yyyy的话,统计相差的年份,截取前4位进行统计
select datediff(yyyy,’20210928′,’20200121′)test
–select datediff(year,’20210928′,’20200121′)test
–select datediff(yy,’20210928′,’20200121′)test
–select datediff(year,’20210928′,’20200121′)test
–select datediff(yy,’20210928′,’20200121′)test
2、参数是 qq时,统计相差的季度
select datediff(qq,’20210928′,’20200121′)test
–select datediff(q,’20210928′,’20200121′)test
–select datediff(quarter,’20210928′,’20200121′)test
–select datediff(q,’20210928′,’20200121′)test
–select datediff(quarter,’20210928′,’20200121′)test
3、参数是 mm的时候,统计相差的月份,截取前6位进行统计
select datediff(mm,’20210928′,’20200121′)test
–select datediff(m,’20210928′,’20200121′)test
–select datediff(month,’20210928′,’20200121′)test
–select datediff(m,’20210928′,’20200121′)test
–select datediff(month,’20210928′,’20200121′)test
4、参数是 dd的时候,统计相差的天数
select datediff(dd,’20210928′,’20200121′)test
–select datediff(day,’20210928′,’20200121′)test
–select datediff(d,’20210928′,’20200121′)test
–select datediff(day,’20210928′,’20200121′)test
–select datediff(d,’20210928′,’20200121′)test
5、参数是 hh的时候,统计相差的小时
select datediff(hour,’20211112 10:00:00′,’20211112 00:00:00′)test_hour
,datediff(hh,’20211112 10:00:00′,’20211112 00:00:00′)test_hh
,datediff(hh,’20211112 10:00:00′,’20211112 00:00:00′)test_hh
6、参数是 mi的时候,统计相差的分钟数
select datediff(minute,’20211112 10:00:00′,’20211112 00:00:00′)test_minute
,datediff(mi,’20211112 10:00:00′,’20211112 00:00:00′)test_mi
,datediff(n,’20211112 10:00:00′,’20211112 00:00:00′)test_n
,datediff(mi,’20211112 10:00:00′,’20211112 00:00:00′)test_mi
,datediff(n,’20211112 10:00:00′,’20211112 00:00:00′)test_n
7、参数是 ss的时候,统计相差的秒数
select datediff(second,’20211112 10:00:00′,’20211112 00:00:00′)test_second
,datediff(ss,’20211112 10:00:00′,’20211112 00:00:00′)test_ss
,datediff(s,’20211112 10:00:00′,’20211112 00:00:00′)test_s
,datediff(ss,’20211112 10:00:00′,’20211112 00:00:00′)test_ss
,datediff(s,’20211112 10:00:00′,’20211112 00:00:00′)test_s
到此这篇关于关于datediff用法的文章就介绍到这了,更多相关datediff用法内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!