Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Stored Procedure If Select

Author  Topic 

macca
Posting Yak Master

146 Posts

Posted - 2012-01-13 : 10:54:34
I have a table called tblStart that contains 100's records. I want to run the following code on all records of tblStart:

IF DateAppealed != NULL AND DateUnAppealed = NULL
BEGIN
UPDATE tblStart
SET John_Total_Days_On_Appeal = DATEDIFF(dd, DateAppealed, GETDATE())
END
ELSE
BEGIN
UPDATE tblStart
SET John_Total_Days_On_Appeal = DATEDIFF(dd, DateAppealed, DateUnAppealed)
END

I keep getting the error that DateAppealed and DateUnAppealed ar unrecognised columns.

Anyone know what I should do?
Thanks.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-01-13 : 11:03:37
[code]
UPDATE tblStart
SET John_Total_Days_On_Appeal
= CASE WHEN DateAppealed IS NOT NULL AND DateUnAppealed IS NULL
THEN DATEDIFF(dd, DateAppealed, GETDATE())
ELSE DATEDIFF(dd, DateAppealed, DateUnAppealed)
END
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-13 : 11:34:09
[code]
UPDATE tblStart
SET John_Total_Days_On_Appeal
=DATEDIFF(dd, DateAppealed,COALESCE(DateUnAppealed,GETDATE()))
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

macca
Posting Yak Master

146 Posts

Posted - 2012-01-17 : 04:12:53
Thanks guys, that worked.
Go to Top of Page
   

- Advertisement -