Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 23rd, 2006, 04:09 AM
Registered User
 
Join Date: Jan 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to vincee
Default calculating time differences

i need to calculate the time differences in minute.
can anybody help me with this?
i have this 2 columns which is:
   -origination_time
   -due_time

i use this function:
  SELECT org_time, due_time,
         (datediff (n, org_time, due_time)) 'mins'

but it calculate in 100 instead of 60

can anyone help me out with this?
thanks
 
Old January 23rd, 2006, 04:51 AM
Registered User
 
Join Date: Jan 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to vincee
Default

need to add on:
those twe columns datatype is int
do i need to convert it to datetime format 1st?
if yes, how?
 
Old January 24th, 2006, 01:29 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

show some sample data and desired results

Jim

 
Old January 24th, 2006, 06:38 AM
Registered User
 
Join Date: Jan 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to vincee
Default

The results:

org_time due_time mins
-------- -------- ------
1629 1630 1
751 800 49
1521 1530 9
1123 1130 7
1133 1145 12
1147 1200 53

Desired result:

org_time due_time mins
-------- -------- ------
1629 1630 1
751 800 9
1521 1530 9
1123 1130 7
1133 1145 12
1147 1200 13

Thank you.
 
Old January 24th, 2006, 07:32 AM
Authorized User
 
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi.

org_time and due_time should naturally be datetimes to account for shifting of dates etc.

However, if that is not possible, I belive this query should work:

Code:
SELECT DATEDIFF(mi,
         CAST('20060101 '+LEFT(RIGHT(('0' + CAST(org_time as varchar)),4),2) + ':'+RIGHT(RIGHT(('0' + CAST(org_time as varchar)),4),2) as datetime),
        CAST('20060101 '+LEFT(RIGHT(('0' + CAST(due_time as varchar)),4),2) + ':'+RIGHT(RIGHT(('0' + CAST(due_time as varchar)),4),2) as datetime)
    )
FROM tblYourTable

I'm sure there is better ways to solve your problem, but I hope this will help.

Gert

 
Old January 24th, 2006, 07:34 AM
Authorized User
 
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just be aware that my sollution doesn't work across different dates. The org_time and due_time must be the same date.

Gert

 
Old January 24th, 2006, 11:02 AM
Authorized User
 
Join Date: Sep 2005
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you only care about times you can also do a kludge like this

create table timespan(startime int,endtime int)
insert into timespan
select 1629 , 1630 union all
select 751 , 800 union all
select 1521 , 1530 union all
select 1123 , 1130 union all
select 1133 , 1145 union all
select 1147 , 1200


select startime org_time,endtime due_time,
case when (endtime-startime) - 40 < 0 then
endtime-startime else (endtime-startime) - 40 end mins
from timespan

“I sense many useless updates in you... Useless updates lead to fragmentation... Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" -- http://sqlservercode.blogspot.com/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculating time Marta Access 0 April 15th, 2008 10:34 PM
Calculating a time difference SQLScott SQL Server 2005 11 October 25th, 2007 02:42 AM
Calculating Time elapsed on form load Brendan Bartley Access 0 June 21st, 2005 09:02 AM
Problems with" i=i+1" and calculating time tronnen XSLT 4 June 6th, 2005 04:17 PM
Missing time part while calculating the Delay happygv SQL Language 2 October 29th, 2003 11:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.