Spencer,
You original code works because it's doing something tricky. The problem is that, because it's tricky, it's also difficult to understand and maintain. I'll resolve it for you step-by step so you can see how it works.
Assume that Birthdate = Feb 12, 1969
You start with this:
Age = @YEAR(@DATE) - @YEAR(BirthDate) - ((@MONTH(@DATE) < @MONTH(BirthDate)) or (@MONTH(@DATE) = @MONTH(BirthDate) AND @DOM(@DATE) < @DOM(BirthDate)))
If we resolve all the @Year, @Month, etc., we end up with this:
Age = 2005 - 1969 - ((3 < 2) or (3 = 2 AND 10 < 12))
Here's where the trick comes in. Each of the boolean expressions below (like 3 < 2) resolves to True (1) or False (0). Since 3 < 2, the first expression below resolves to False or 0. The whole things resolves as follows:
Age = 2005 - 1969 - ((0) or (0 AND 1))
The last expression is (0 AND 1). For an expression like this which uses AND to be True, all it's parts must be True. In this case, one of the parts is False (0), so the whole thing resolves to 0.
Age = 2005 - 1969 - ((0) or (0))
The last remaining expression uses OR. If any of the parts are True, the whole thing is True. In this case, both parts are False, so the whole things resolves to 0.
Age = 2005 - 1969 - 0
Now, you have resolved all the expressions except the final subtraction. Notice that the final number is 0. If any of the bits above had been True, the last number would have been 1.
Age = 36
That's how the trick works.
The method Ray used is less slick, but it's also more obvious and easier to read. In specific, however, there is a condition reversed, so you're getting funny results. Give this a try:
If Not @IsBlank(BirthDate) Then
{
If (@Month(BirthDate) < @Month(@Date)) Or ((@Month(BirthDate) = @Month(@Date)) And (@DOM(BirthDate) <= @DOM(@Date)))
{
Age = @Year(@Date) - @Year(BirthDate)
}
Else
{
Age = @Year(@Date) - @Year(BirthDate) - 1
}
}
Or this:
var vOffset as Int
vOffset = 0
If Not @IsBlank(BirthDate) Then
{
If (@Month(@Date) < @Month(BirthDate)) Or ((@Month(@Date) = @Month(BirthDate)) And (@DOM(@Date) < @DOM(BirthDate)))
{
vOffset = 1
}
Age = @Year(@Date) - @Year(BirthDate) - vOffset
}