Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 need to minus dates

Author  Topic 

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2010-04-26 : 05:41:19
Hi all

I am not able to create a code which may minus date 1 to date 2.
Date1 Date2
2010-04-23 08:12:55.000 2010-04-23 08:18:50.000
2010-04-23 08:18:51.000 2010-04-23 08:26:50.000
2010-04-23 08:26:51.000 2010-04-23 08:55:30.000
2010-04-23 08:57:05.000 2010-04-23 09:10:32.000
2010-04-23 08:58:48.000 2010-04-23 09:10:32.000


I have table t1 which has two date columns and others ...i have requirement in which i need to minus from date1 from date2 like this

(2010-04-23 08:18:50.000)-(2010-04-23 08:18:51.000)

next is

(2010-04-23 08:26:50.000)-(2010-04-23 08:26:51.000)

and so till the last


(2010-04-23 09:10:32.000)-(2010-04-23 08:58:48.000)


means from the first date value of DATE2 columns to second date value of DATE1 columns and it should be so on...


i think i explanined my problem properly


khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-04-26 : 05:48:39
quote:
I have table t1 which has two date columns and others ...i have requirement in which i need to minus from date1 from date2 like this

(2010-04-23 08:18:50.000)-(2010-04-23 08:18:51.000)

You want the date different in second ? minutes ? hour ?
basically you can use datediff(<datepart>, <date 1>, <date 2>) and specify the corresponding <datepart> paramter

Refer to BOL http://msdn.microsoft.com/en-us/library/ms189794.aspx

Are you using SQL Server 2000 ? 2005 / 2008 ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2010-04-26 : 05:55:11
THANKS FOR YOUR REPLY ...I NEED IN HOUR ...

BUT IT SHOULD BE LIKE THIS DATE2(FIRST VALUE)-DATE1(SECOND VALUE)
AND AGIAN
DATE2(SECOND VALUE)-DATE1(THIRD VALUE)
NOW AGAIN
DATE2(THIRD VALUE)-DATE1(FOURTH VALUE)

AND SO ON....TILL THE LAST RECORD
I AM TRYING TO CREATE CURSOR BUT NOT ABLE TO BUILD THE LOGIC..pLEASE HELP ME


quote:
Originally posted by khtan

quote:
I have table t1 which has two date columns and others ...i have requirement in which i need to minus from date1 from date2 like this

(2010-04-23 08:18:50.000)-(2010-04-23 08:18:51.000)

You want the date different in second ? minutes ? hour ?
basically you can use datediff(<datepart>, <date 1>, <date 2>) and specify the corresponding <datepart> paramter

Refer to BOL http://msdn.microsoft.com/en-us/library/ms189794.aspx

Are you using SQL Server 2000 ? 2005 / 2008 ?


KH
[spoiler]Time is always against us[/spoiler]



Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-04-26 : 05:59:24
[code]
declare @sample table
(
Date1 datetime,
Date2 datetime
)
insert into @sample
select '2010-04-23 08:12:55.000', '2010-04-23 08:18:50.000' union all
select '2010-04-23 08:18:51.000', '2010-04-23 08:26:50.000' union all
select '2010-04-23 08:26:51.000', '2010-04-23 08:55:30.000' union all
select '2010-04-23 08:57:05.000', '2010-04-23 09:10:32.000' union all
select '2010-04-23 08:58:48.000', '2010-04-23 09:10:32.000'

; with
cte
as
(
select Date1, Date2, row_no = row_number() over (order by Date1)
from @sample
)
select c1.Date1, c1.Date2, datediff(minute, c1.Date2, c2.Date1) / 60.0
from cte c1
left join cte c2 on c1.row_no = c2.row_no - 1

/*
Date1 Date2
------------------------------------------------------ ------------------------------------------------------ -------------------
2010-04-23 08:12:55.000 2010-04-23 08:18:50.000 .000000
2010-04-23 08:18:51.000 2010-04-23 08:26:50.000 .000000
2010-04-23 08:26:51.000 2010-04-23 08:55:30.000 .033333
2010-04-23 08:57:05.000 2010-04-23 09:10:32.000 -.200000
2010-04-23 08:58:48.000 2010-04-23 09:10:32.000 NULL

(5 row(s) affected)
*/
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2010-04-26 : 06:31:31
Thank you very much for your quick response
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-26 : 06:38:18
[code]SELECT DATEDIFF(minute,t1.Date2,t.Date1)/60.0
FROM Table t
OUTER APPLY (SELECT TOP 1 Date2
FROM Table
WHERE Date2 < t.Date1
ORDER BY Date2 DESC)t1
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-04-26 : 07:29:50
[code]
;WITH c1 AS
(SELECT k=1, Date1, row_id = ROW_NUMBER() OVER(ORDER BY (SELECT 0))
FROM @sample),
c2 AS
(SELECT k=2, Date2, row_id = ROW_NUMBER() OVER(ORDER BY (SELECT 0)) + 1
FROM @sample)
SELECT ABS(DATEDIFF(minute, MAX(CASE WHEN k=1 THEN d END), MAX(CASE WHEN k=2 THEN d END))/60.0)
FROM
(
SELECT * FROM c1
UNION ALL
SELECT * FROM c2
)D(k,d,r)
GROUP BY r
[/code]
Go to Top of Page
   

- Advertisement -