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.
BauerchickAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

Anthony PerkinsCommented:
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
0
Imthiyaz_phCommented:
Hi acperkins

Wouldnt the above trigger fire recursively? I have a doubt.

CREATE TRIGGER trg_MyTrigger On YourTableName
For INSERT, UPDATE
As
If Update(CONTACTDATE)
Begin
  Update     YourTableName
  Set [WEEK] = DATEPART(week, CONTACTDATE)
  From     YourTableName t
        Inner Join Inserted i On t.YourPrimaryKeyHere = i.YourPrimaryKeyHere
End


If the column is in the same table, i would create a computed column with the formula "DATEPART(week, CONTACTDATE)" instead of using a trigger.
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
BauerchickAuthor Commented:
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.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Anthony PerkinsCommented:
>>Wouldnt the above trigger fire recursively? <<
By default Recursive triggers are off.
0
Imthiyaz_phCommented:
Sorry acperkins, i forgot abt that option. Thank u.
0
Anthony PerkinsCommented:
>>Sorry acperkins, i forgot abt that option. Thank u.<<
No problem.  And you are absolutely correct about being cautious.
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
Microsoft SQL Server

From novice to tech pro — start learning today.