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
 General SQL Server Forums
 New to SQL Server Programming
 Microsoft SQL Update - HELP!!!

Author  Topic 

rhall47
Starting Member

2 Posts

Posted - 2011-09-02 : 14:37:32
Hello, I'm new here but desperately need your help. I'm trying to put together a simple script to update a database table with a Rubgy Clubs Junior Team assignment.

Each player has a record with the Date of Birth and the team that he has been assigned to based upon his age at the 1st September 2011. I have the beginings of the script but I'm not sure if you can use 'IF' statements that calculate the number of years:-


DECLARE @TARGETDATE smalldatetime
DECLARE @AGE int

UPDATE [Membership].[dbo].[Juniors]

SET @TARGETDATE = '2011-09-01 00:00:00'
SET @AGE = 0

IF DATEADD(YEAR, DATEDIFF (YEAR, [JnrDOB], @TARGETDATE), [JnrDOB]) > @TARGETDATE BEGIN
SET @AGE = DATEDIFF(YEAR, [JnrDOB], @TARGETDATE) - 1
END

IF DATEADD(YEAR, DATEDIFF (YEAR, [JnrDOB], @TARGETDATE), [JnrDOB]) < @TARGETDATE BEGIN
SET @AGE = DATEDIFF(YEAR, [JnrDOB], @TARGETDATE)
END

Now having calculated the number of years in'@AGE' I want to write that into the appropriate table field.

Please help

Richard

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-02 : 14:41:10
Why are you storing the age? Age should be calculated and never stored. You can't use IF in there, you'd have to use CASE expression instead, but you should really be avoiding this altogether.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rhall47
Starting Member

2 Posts

Posted - 2011-09-02 : 14:43:44
Hi Tara,

It's not so much the age, I do understand your point but the team which the player has been assigned too based on his age as at the 1st of September

This would then say his was assigned to the "Under 7's" or "Under 8's" etc
Go to Top of Page
   

- Advertisement -