Bauerchick
asked on
DATEPART and TRIGGER to post WEEK NUMBER
How can I write a trigger to take the datetime object in one field - ie CONTACTDATE - and calculate the week of that date and update the field WEEK with the weeknumber of that date? I think it will use DATEPART(ww, contactdate) or something like that? I really don't know sql very well.
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 to both of you, but I found that the calculation idea worked best. A big thanks to Imthiyaz_ph for the quick and extremely simple answer.
>>Wouldnt the above trigger fire recursively? <<
By default Recursive triggers are off.
By default Recursive triggers are off.
Sorry acperkins, i forgot abt that option. Thank u.
>>Sorry acperkins, i forgot abt that option. Thank u.<<
No problem. And you are absolutely correct about being cautious.
No problem. And you are absolutely correct about being cautious.
CREATE TRIGGER trg_MyTrigger On YourTableName
For INSERT, UPDATE
As
Update YourTableName
Set [WEEK] = DATEPART(week, CONTACTDATE)
From YourTableName t
Inner Join Inserted i On t.YourPrimaryKeyHere = i.YourPrimaryKeyHere