Link to home
Start Free TrialLog in
Avatar of Bauerchick
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.
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Something like this perhaps:


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
ASKER CERTIFIED SOLUTION
Avatar of Imthiyaz_ph
Imthiyaz_ph

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bauerchick
Bauerchick

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.
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.