sql日期时间相减语句

sql日期时间相减语句本款教程利用了datediff函数,来对数据库的日子举行相减查询哦,下边便写了N种关于mssql
日期相减的措施。sql中三个日子相减

   sql日期时间相减语句

sql日期时间相减语句本款教程利用了datediff函数,来对数据库教程的日子进行相减查询哦,下面便写了n种关于mssql
日期相减的措施。sql中三个日子相减 1、相差天数 select
trunc(sysdate,’yyyy’卡塔尔国-to_date(‘2010-01-04′,’yyyy-mm-dd’卡塔尔(قطر‎ from dual;
2、相差月数 select
months_between(trunc(sysdate,’mm’),to_date(‘2010-01′,’yyyy-mm’State of Qatar卡塔尔(قطر‎ from
dual; 3、相差年数 select
trunc((months_between(trunc(sysdate,’dd’),to_date(‘2009-01-01′,’yyyy-mm-dd’)))/12)
from dual;

  本款教程利用了datediff函数,来对数据库教程的日期举办相减查询哦,下边便写了n种关于mssql
日期相减的办法。

4、select datediff( day, ‘2008.08.25’, ‘2008.09.01’ )

  sql中四个日子相减

5、select datediff( second, 二〇一〇-8-25 12:15:12′, 二〇一〇-9-1 7:18:20’State of Qatar–重回相差秒数6、select datediff( minute, 二零一零-8-25 12:15:12′, 二零一零-9-1
7:18:20’卡塔尔国 –重临相差分钟数7、select datediff( hour, 二〇〇九-8-25 12:15:12′,
2010-9-1 7:18:20’卡塔尔国 –重回相差时辰数

  1、相差天数

问题三:select datediff( day, 2009-8-25 12:15:12′, 2009-9-1 7:18:20′)

  select trunc(sysdate,’yyyy’)-to_date(‘2009-01-04′,’yyyy-mm-dd’)
from dual;

实例二

  2、相差月数

use pubsselect distinct datediff(day, ‘2009-3-12’, ‘2009-3-15’) as
difdayfrom titles结果:3

  select
months_between(trunc(sysdate,’mm’),to_date(‘2009-01′,’yyyy-mm’)) from
dual;

declare @dt1 as datetime, @dt2 as datetime; select @dt1 = ‘2008-8-4
9:36:41’, @dt2 = ‘2008-8-2 9:33:39’;

  3、相差年数

declare @days as int, @hours as int, @minutes as int, @seconds as int;

  select
trunc((months_between(trunc(sysdate,’dd’),to_date(‘2009-01-01′,’yyyy-mm-dd’)))/12)
from dual;

set @seconds = datediff( second, @dt2, @dt1); set @days = @seconds / (24
* 60 * 60) set @seconds = @seconds – @days * 24 * 60 * 60 set
@hours = @seconds / (60 * 60); set @seconds = @seconds – @hours * 60
* 60 set @minutes = @seconds / 60; set @seconds = @seconds – @minutes
* 60; select convert(varchar(10), @days ) + ‘天’ + convert(varchar(10),
@hours ) + ‘小时’ + convert(varchar(10), @minutes ) + ‘分’ +
convert(varchar(10), @seconds ) + ‘秒’;

  4、select datediff( day, ‘2008.08.25’, ‘2008.09.01’ )

上面来看个实例

  5、select datediff( second, 二〇〇八-8-25 12:15:12′, 二零零六-9-1 7:18:20’卡塔尔(قطر‎–重返相差秒数

本身有二个表,此中有多个字段:伊始天数,初叶时间,到达天数,到达时间(那三个字段都是varchar类型卡塔尔

  6、

譬喻说:某一条记下: 1 16:00 2 12:20

  select datediff( minute, 2009-8-25 12:15:12′, 二零零六-9-1 7:18:20’卡塔尔–重回相差分钟数

自个儿的指标就是用 select-(开头天数+开首时间卡塔尔 as 开销时间 from table

  7、

举个例子上条记下得到的便是 -(24:00+16:00卡塔尔国=20:00

  select datediff( hour, 二〇〇九-8-25 12:15:12′, 二〇〇九-9-1 7:18:20’State of Qatar–重返相差时辰数

那般的sql语句该怎么写???

  问题三:select datediff( day, 2009-8-25 12:15:12′, 2009-9-1
7:18:20′)

declare @t table (beginday int,begintime varchar(20),endday int,endtime
varchar(20))

  实例二

insert @t select 1,’16:00′,2,’12:20’union all select 1,’3:00′,3,’19:10′

  use pubs

select date=rtrim(date/60)+’:’+rtrim(date%60)from (select
date=datediff(mi,1,dateadd(d,endday-beginday,beginday)-begintime+endtime)from
@t )t

  select distinct datediff(day, ‘2009-3-12’, ‘2009-3-15’) as difday

date————————-20:2064:10

  from titles

方法二

  结果:3

declare @t table(开首天数 varchar(10卡塔尔(قطر‎,开首时间 varchar(10State of Qatar,达到天数
varchar(10卡塔尔国,达到时间 varchar(10卡塔尔(قطر‎卡塔尔(قطر‎insert @t select ‘1’,
’16:00′,’2′,’12:20′

  declare @dt1 as datetime, @dt2 as datetime;

–纵然初阶天数,达到天数大于31select 到达天数 * 24 +
datepart(hh,达到时间State of Qatar – 开端天数 * 24 – datepart(hh,伊始时间卡塔尔(قطر‎from @t

  select @dt1 = ‘2008-8-4 9:36:41’, @dt2 = ‘2008-8-2 9:33:39’;

  declare @days as int, @hours as int, @minutes as int, @seconds as
int;

  set @seconds = datediff( second, @dt2, @dt1);

  set @days = @seconds / (24 * 60 * 60)

  set @seconds = @seconds – @days * 24 * 60 * 60

  set @hours = @seconds / (60 * 60);

  set @seconds = @seconds – @hours * 60 * 60

  set @minutes = @seconds / 60;

  set @seconds = @seconds – @minutes * 60;

Leave a Comment.