rsmuckles
asked on
Derive date from two (or more) existing dates - Sql Server
I have a situation in which I'm expected to derive end dates for a record from a single field for a variable amount of records...and if the record has no next record then the final record = '2099-12-31'
For instance this:
Member Identify_Date
14 2011-01-01
14 2011-02-14
14 2011-03-01
Needs to be coded to end up as this
Member Identify_Date End_Date
14 2011-01-01 2011-02-13
14 2011-02-14 2011-02-28
14 2011-03-01 2099-12-31
I've been working on this all day. Can't figure it out. Thanks so much for your help.
Julia
For instance this:
Member Identify_Date
14 2011-01-01
14 2011-02-14
14 2011-03-01
Needs to be coded to end up as this
Member Identify_Date End_Date
14 2011-01-01 2011-02-13
14 2011-02-14 2011-02-28
14 2011-03-01 2099-12-31
I've been working on this all day. Can't figure it out. Thanks so much for your help.
Julia
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks so much Sharath. I'd never heard of CTE so I looked it up and this was the best explanation I could find for those of you who don't know.
http://www.simple-talk.com/sql/t-sql-programming/sql-server-cte-basics/
I really appreciate the help.
http://www.simple-talk.com/sql/t-sql-programming/sql-server-cte-basics/
I really appreciate the help.
Open in new window
Now, what you need to do is LEFT OUTER JOIN the results of that query to the original table and then COALESCE the End_Date with "2099-12-31".
Open in new window