Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 9th, 2007, 05:03 PM
Registered User
 
Join Date: Mar 2007
Location: Dunmore, Pa, USA.
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


Reply With Quote
  #2 (permalink)  
Old March 12th, 2007, 10:34 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

How about a Cursor...?

_________________________
- Vijay G
Strive for Perfection
Reply With Quote
  #3 (permalink)  
Old March 14th, 2007, 01:45 PM
Friend of Wrox
 
Join Date: Aug 2004
Location: Orange County, CA, USA.
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)


Reply With Quote
  #4 (permalink)  
Old March 14th, 2007, 04:46 PM
Friend of Wrox
 
Join Date: Aug 2004
Location: Orange County, CA, USA.
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.


Reply With Quote
  #5 (permalink)  
Old March 15th, 2007, 07:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
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
Reply With Quote
  #6 (permalink)  
Old March 20th, 2007, 08:47 AM
Registered User
 
Join Date: Mar 2007
Location: Dunmore, Pa, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Guys I appreciate you help....

Reply With Quote
  #7 (permalink)  
Old March 20th, 2007, 09:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
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
Reply With Quote
  #8 (permalink)  
Old March 22nd, 2007, 04:45 PM
Friend of Wrox
 
Join Date: Aug 2004
Location: Orange County, CA, USA.
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




Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:15 AM.


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