Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating null dates to all be different by 1 millisecond

Status
Not open for further replies.

boardtc

Programmer
Dec 1, 2004
22
I need all the null dates to be different by 1 millisecond. I try

declare @date datetime
set @date = dateadd(week, -2, getdate())

update conditions
set LastUpdated = dateadd(millisecond,1,@date)
where (LastUpdated is NULL)

I can't see how I can do it without some loop.

Could I do it in conjunction with adding the "row number"? :
select RowNumber = (select count(*)
from conditions as a2
where a2.oid <= a1.oid)
from conditions as a1

Any other ideas?

Thanks, Tom.
 
btw, 1 millisecond is not important, a difference of 1 second or maybe 1 minute would be fine too
 
Got it !

declare @date datetime
set @date = dateadd(week, -2, getdate())

update conditions
set @date = LastUpdated = dateadd(s, 1, @date)
where (LastUpdated is NULL)
 
Yes that will work, but it's best not to use the double = construct. It can lead to unpredictable results. If you don't add any more columns to the Set clause, you should be ok as is.
Code:
[Blue]DECLARE[/Blue] @Cnt [Blue]AS[/Blue] [Blue]int[/Blue]
[Blue]SET[/Blue] @Cnt[Gray]=[/Gray]1
   [Blue]UPDATE[/Blue] Conditions
      [Blue]SET[/Blue] @Cnt[Gray]=[/Gray]@Cnt[Gray]+[/Gray]1[Gray],[/Gray] LastUpdated [Gray]=[/Gray] [Fuchsia]DateAdd[/Fuchsia][Gray]([/Gray]millisecond[Gray],[/Gray]@Cnt[Gray],[/Gray]@date[Gray])[/Gray]
   [Blue]WHERE[/Blue] [Gray]([/Gray]LastUpdated [Blue]IS[/Blue] [Gray]NULL[/Gray][Gray])[/Gray]
-Karl



[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top