Help With Date/Time Query

  • Hi, I need help with a date/time query.

    I have four fields, startdate, starttime, enddate, endtime. I need to calculate the difference between the startdate/time and the enddate/time. For example, 09/07/2006 10:00AM and 09/07/2006 11:00AM is 60 minutes. I can't figure out how to phrase this.

    declare @startdate datetime

    declare @starttime datetime

    declare @enddate datetime

    declare @endtime datetime

    set @startdate = '09/07/2006'

    set @starttime = '10:00AM'

    set @enddate = '09/07/2006'

    set @endtime = '11:00AM'

    declare @start datetime

    declare @end datetime

    set @start = @startdate + @starttime

    set @end = @enddate + @endtime

    declare @minutes

    set @minutes = @end-@start

  • Hope this one solves your problem.

    declare @startdate datetime

    declare @starttime datetime

    declare @enddate datetime

    declare @endtime datetime

    Declare @startdatetime datetime,@enddatetime datetime

    set @startdate = '09/07/2006'

    set @starttime = '10:00AM'

    set @enddate = '09/07/2006'

    set @endtime = '11:20AM'

    Select @startdatetime = DateAdd(ss,DatePart(ss,@starttime),DateAdd(mi,DatePart(mi,@starttime),DateAdd(hh,DatePart(hh,@starttime),@startdate)))

    Select @enddatetime = DateAdd(ss,DatePart(ss,@endtime),DateAdd(mi,DatePart(mi,@endtime),DateAdd(hh,DatePart(hh,@endtime),@enddate)))

    select @startdatetime,@enddatetime,Datediff(mi,@startdatetime,@enddatetime)

    Thanks

    Sreejith

  • Hi together,

    i wonder if you have considered using the SQL Datediff() function? Look it up in help online, it offers convenient access to what you may need.

    Cheers,

    Juergen

  • Just change the last line to:

    set @minutes = DateDiff(mi, @start, @end)

  • Thanks to all for the suggestions.

    I got my query to work!!

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply