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 March 9th, 2007, 05:03 PM
Registered User
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default compare values in one row to the next

help, help!!! I am trying to write sql script that will get the datediff from the bl_term_dt of the 1st row and the bl_eff_dt of the 2nd row. then get the datediff of the bl_term_dt of the 2nd row and bl_eff_dt of the 3rd row and so on. Any suggestions?

bl_ck bl_eff_dt bl_term_dt
52790 2006-01-01 00:00:00.000 2006-03-31 00:00:00.000
52790 2006-04-01 00:00:00.000 2006-06-30 00:00:00.000
52790 2006-07-01 00:00:00.000 2007-01-31 00:00:00.000
52790 2007-02-01 00:00:00.000 9999-12-31 00:00:00.000


 
Old March 12th, 2007, 10:34 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

How about a Cursor...?

_________________________
- Vijay G
Strive for Perfection
 
Old March 14th, 2007, 01:45 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Happyqv has the right idea. Select the data into a cursor in the particular order your looking for. Then step through the data row by row. Saving the previous rows data into variables. Then compare if the variable (previous row value) is the same as the current row value.

(Just expanding on happyqv's comment)


 
Old March 14th, 2007, 04:46 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I just posted a similar cursor related question. If you want take a look at that code in the other post.


 
Old March 15th, 2007, 07:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Rob, it would help him better if you posted the link of that thread here...

In fact it is not happyQv, it is happyGv stands for "G Vijay" ;) no harm in that being mispelt

Cheers.

_________________________
- Vijay G
Strive for Perfection
 
Old March 20th, 2007, 08:47 AM
Registered User
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Guys I appreciate you help....

 
Old March 20th, 2007, 09:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

So, did you manage to get it solved? If so what is that you did to accomplish that? How about sharing it here.

cheers.

_________________________
- Vijay G
Strive for Perfection
 
Old March 22nd, 2007, 04:45 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

-- Try this logic.

Create table datecompare (bl_ck int, bl_eff_dt datetime, bl_term_dt datetime)
go
insert into datecompare (bl_ck, bl_eff_dt, bl_term_dt) values (52790, convert(datetime,'2006-01-01 00:00:00.000'), convert(datetime,'2006-03-31 00:00:00.000'))
insert into datecompare (bl_ck, bl_eff_dt, bl_term_dt) values (52790, convert(datetime,'2006-04-01 00:00:00.000'), convert(datetime,'2006-06-30 00:00:00.000'))
insert into datecompare (bl_ck, bl_eff_dt, bl_term_dt) values (52790, convert(datetime,'2006-07-01 00:00:00.000'), convert(datetime,'2007-01-31 00:00:00.000'))
insert into datecompare (bl_ck, bl_eff_dt, bl_term_dt) values (52790, convert(datetime,'2007-02-01 00:00:00.000'), convert(datetime,'9999-12-31 00:00:00.000'))
go

Declare @Prev_bl_eff_dt datetime
Declare @Prev_bl_term_dt datetime
Declare @Cur_bl_ck int
Declare @Cur_bl_eff_dt datetime
Declare @Cur_bl_term_dt datetime
Declare @PrintMe Varchar(1200)

DECLARE X_Cursor CURSOR FOR
select bl_ck, bl_eff_dt, bl_term_dt from datecompare order by bl_ck, bl_eff_dt, bl_term_dt

OPEN X_Cursor
   FETCH NEXT FROM X_Cursor INTO @Cur_bl_ck, @Cur_bl_eff_dt, @Cur_bl_term_dt

set @Prev_bl_eff_dt = @Cur_bl_term_dt
-- set @Prev_bl_term_dt = @Cur_bl_term_dt

WHILE @@FETCH_STATUS = 0
   BEGIN
         set @PrintMe = 'Current bl_ck = ' + convert(char(24),@Cur_bl_ck) + '||'
         set @PrintMe = @PrintMe + 'Current bl_eff_dt = ' + convert(char(12),@Cur_bl_eff_dt,101) + '||'
         set @PrintMe = @PrintMe + 'Current bl_eff_dt = ' + convert(char(12),@Cur_bl_eff_dt,101) + '||'
         set @PrintMe = @PrintMe + 'Current bl_eff_dt = ' + convert(char(12),@Cur_bl_term_dt,101) + '||'
         set @PrintMe = @PrintMe + 'Date Delta = ' + convert(char(12),dateDiff(day,@Prev_bl_term_dt,isn ull(@Cur_bl_eff_dt,@Prev_bl_term_dt))) + '||'

         set @Prev_bl_eff_dt = @Cur_bl_eff_dt
         set @Prev_bl_term_dt = @Cur_bl_term_dt

         Print @Printme
      FETCH NEXT FROM X_Cursor INTO @Cur_bl_ck, @Cur_bl_eff_dt, @Cur_bl_term_dt

   END
CLOSE X_Cursor
DEALLOCATE X_Cursor
go









Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare values in different nodes fann XSLT 9 September 11th, 2008 06:25 AM
How to compare two lat/long values chandup SQL Server 2000 1 May 17th, 2007 05:07 AM
Compare "before and after" values on a form? wayne62682 Classic ASP Basics 3 May 17th, 2006 03:13 AM
compare the values in a select box ayse1st BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 0 January 7th, 2006 03:58 PM
Compare values of several drop boxes, notify user SoC Javascript How-To 1 April 21st, 2005 10:17 AM





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