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.
| Author |
Topic |
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2010-04-26 : 05:41:19
|
| Hi allI am not able to create a code which may minus date 1 to date 2. Date1 Date22010-04-23 08:12:55.000 2010-04-23 08:18:50.0002010-04-23 08:18:51.000 2010-04-23 08:26:50.0002010-04-23 08:26:51.000 2010-04-23 08:55:30.0002010-04-23 08:57:05.000 2010-04-23 09:10:32.0002010-04-23 08:58:48.000 2010-04-23 09:10:32.000I 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> paramterRefer to BOL http://msdn.microsoft.com/en-us/library/ms189794.aspxAre you using SQL Server 2000 ? 2005 / 2008 ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
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 MEquote: 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> paramterRefer to BOL http://msdn.microsoft.com/en-us/library/ms189794.aspxAre you using SQL Server 2000 ? 2005 / 2008 ? KH[spoiler]Time is always against us[/spoiler]
|
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-04-26 : 05:59:24
|
[code]declare @sample table( Date1 datetime, Date2 datetime)insert into @sampleselect '2010-04-23 08:12:55.000', '2010-04-23 08:18:50.000' union allselect '2010-04-23 08:18:51.000', '2010-04-23 08:26:50.000' union allselect '2010-04-23 08:26:51.000', '2010-04-23 08:55:30.000' union allselect '2010-04-23 08:57:05.000', '2010-04-23 09:10:32.000' union allselect '2010-04-23 08:58:48.000', '2010-04-23 09:10:32.000'; withcteas( 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.0from 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 .0000002010-04-23 08:18:51.000 2010-04-23 08:26:50.000 .0000002010-04-23 08:26:51.000 2010-04-23 08:55:30.000 .0333332010-04-23 08:57:05.000 2010-04-23 09:10:32.000 -.2000002010-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] |
 |
|
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2010-04-26 : 06:31:31
|
| Thank you very much for your quick response |
 |
|
|
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.0FROM Table tOUTER APPLY (SELECT TOP 1 Date2 FROM Table WHERE Date2 < t.Date1 ORDER BY Date2 DESC)t1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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] |
 |
|
|
|
|
|
|
|