字段:    START_DATE START_TIME END_DATE END_TIME TIME_YU
             20041019   235625     20041020 000236   (这里要得出值)2004-10-19 23:56:25~20041020 00:02:36 这一个时间段,在00:00:00~07:00:00这个时间段内有多少秒时间?求SQL语句

解决方案 »

  1.   

    起始日期START_DATE    -       载止日期END_DATE  = 天数
    天数*24*60*60=总秒数
    总秒数+START_TIME     -       END_TIME = 秒差一点想法
      

  2.   

    select DATEDIFF ( ss , startdate , enddate ) as diffSecond from...
      

  3.   

    DATEDIFF ( ss , startdate , enddate )
      

  4.   

    update hd1120 set time_yu = 
    case when
    (开始时间> 07:00:00 and 开始时间<24:00:00 and 结束时间>07:00:00 and 结束时间<24:00:00)
    then 0
    when
    (开始时间> 07:00:00 and 开始时间<24:00:00  and 结束时间>第二天的00:00:00 and 结束时间<第二天的07:00:00)
    then 结束时间-午夜时间
    when
    (开始时间> 00:00:00 and 开始时间<07:00:00  and 结束时间>00:00:00 and 结束时间<07:00:00)
    then 结束时间-开始时间
    when
    (开始时间> 00:00:00 and 开始时间<07:00:00  and 结束时间>07:00:00 and 结束时间<24:00:00)
    then 07:00:00-开始时间
    end
    from
    hd1120
      

  5.   

    case @i=
        DATEDIFF(ss,
       case when START_TIME<'070000' then CAST((START_DATE+' '
      + REFT(START_TIME,2) +':' + SUBSTRING(START_TIME,3,2) +':' + SUBSTRING(START_TIME,5,2))   as datetime  else DATEADD(dd,CAST(START_DATE as DATETIME),1) end ,
       case when END_TIME<'070000' then CAST((END_DATE+' '
      + REFT(END_TIME ,2) +':' + SUBSTRING(END_TIME ,3,2) +':' + SUBSTRING(END_TIME ,5,2))    as datetime  else CAST(END_DATE + '07:00:00')as DATETIME) end )
     when @i>0 then @i else 0 ;
      

  6.   

    稍稍修改了一下:update hd1120 set time_yu = 
    DATEDIFF(ss,
    case when START_TIME<'070000' 
    then CAST(START_DATE+' '
       + LEFT(START_TIME,2) +':'
       + SUBSTRING(START_TIME,3,2) +':'
       + SUBSTRING(START_TIME,5,2) as datetime)
    else DATEADD(dd,1,CAST(START_DATE as DATETIME)) end,
    case when END_TIME<'070000' 
    then CAST(END_DATE+' '
       + LEFT(END_TIME ,2) +':' 
       + SUBSTRING(END_TIME ,3,2) +':' 
       + SUBSTRING(END_TIME ,5,2) as datetime)
    else CAST((END_DATE + ' 07:00:00')as DATETIME) end)
    where tel like '0%'结贴了。