SQL Update field if NULL with value from prior record

I have a table with a date field called Tech1Pickup. If Tech1Pickup is NULL, I would like the value to be updated with the last prior value, for example, the first 4 NULLs in the below example should be updated to 10/1/14, the 2nd NULL field should show 10/6/14, the next 4 should show 10/8/14.

CURRENT:
Tech1Pickup
10/1/14
NULL
NULL
NULL
NULL
10/6/14
NULL
10/8/14
NULL
NULL
NULL
NULL
10/13/14
NULL

DESIRED RESULT:
Tech1Pickup
10/1/14
10/1/14
10/1/14
10/1/14
10/1/14
10/6/14
10/6/14
10/8/14
10/8/14
10/8/14
10/8/14
10/8/14
10/13/14
10/13/14

Thank you so much!
Norm-alNetwork EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
Which version of SQL Server?
0
Norm-alNetwork EngineerAuthor Commented:
2005
0
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
Are the dates you want in ascending order? For example, would you ever get this?

10/1/14
NULL
NULL
NULL
NULL
9/30/14
NULL
10/8/14
NULL
NULL
NULL
NULL
10/13/14
NULL
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Norm-alNetwork EngineerAuthor Commented:
They are always in ascending order
0
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
CREATE TABLE [dbo].[MyTable](
	[Tech1Pickup] [date] NULL,
	[ID] [int] IDENTITY(1,1) NOT NULL)

with CalcTable as (
select M.Tech1PickUp as CurrentTech1PickUp, M.id, max(N.Tech1PickUp) as NewTech1PickUp
from dbo.MyTable M
left join dbo.MyTable N
on M.id > N.id
group by M.Tech1PickUp, M.id)

select coalesce(CurrentTech1PickUp, NewTech1PickUp) as Tech1PickUp
from CalcTable 
order by id

Open in new window

0
Norm-alNetwork EngineerAuthor Commented:
I only showed the one field but this is in a Calendar table already with multiple other fields... I notice this starts with Create Table.. what is the alternative?
0
mlmccCommented:
Will the first record in your record set ever have a NULL date?
If so what value do you want used?

I assume you are trying to do this in a Delphi program

The basic algorithm would be

Open your recordset
Set LastDate = DefaultDate
Loop through the recordset
    If (Tech1Pickup IS NOT NULL) then
       Set LastDate = Tech1Pickup
    Else
         Set Tech1Pickup = LastDate
         Update record
    EndIF
    Get NEXT RECORD
End LOOP

mlmcc
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
What is your current table structure?
0
Norm-alNetwork EngineerAuthor Commented:
ID      decimal(18, 0)      Unchecked
CalendarDays      datetime      Checked
WeekDays      nvarchar(50)      Checked
Tech1Pickup      datetime      Checked
Tech2Pickup      datetime      Checked
Tech3Pickup      datetime      Checked
Tech4Pickup      datetime      Checked
OfficeWorkdays      nvarchar(50)      Checked
0
Norm-alNetwork EngineerAuthor Commented:
For every calendar day, each tech picks up 2 times a week.. for example Tech1 picks up on Mon and Wed if the calendar day is a Tuesday, the date to show for pickup date should be the previous Mon.  I have the dates entered in, I just need to fill in the blanks..
0
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
You will need to alter the dbo.MyTable name, but here is the code:

with CalcTable as (
select M.Tech1PickUp as CurrentTech1PickUp, M.id, max(N.Tech1PickUp) as NewTech1PickUp
from dbo.MyTable M
left join dbo.MyTable N
on M.id > N.id
group by M.Tech1PickUp, M.id)

select coalesce(CurrentTech1PickUp, NewTech1PickUp) as Tech1PickUp
from CalcTable 
order by id

Open in new window

0
Sinisa VukSoftware architectCommented:
if your primary key is in sequential order and dates are growing as ID field .....
update YourTable set Tech1Pickup=(select max(yt.Tech1Pickup) from YourTable as yt
where yt.ID<YourTable.ID);

Open in new window

0
Geert GOracle dbaCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.