Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Proper Comparisons for Dates in Merge Fields.

Proper Comparisons for Dates in Merge Fields

Written by Allen Wyatt (last updated June 10, 2017)
This tip applies to Word 97, 2000, 2002, and 2003


2

Volker is having a problem comparing dates in a merge field. His database contains a Birthdate field, which (obviously) contains a birth date for each person in the database. As he merges each record, he wants to compare the Birthdate field to a reference date, and then take some action based on the result. The logic he is attempting to use looks like this:

{IF {MERGEFIELD Birthdate} > "02/26/1981" "Under 30" "Over 30"}

The problem is that the comparison doesn't work properly; it doesn't return consistent results. One possible solution is to use a format switch in the merge field, in the following manner:

{IF {MERGEFIELD Birthdate\@"MM/DD/YYYY"} > "02/26/1981" "Under 30" "Over 30"}

This puts the incoming information into a comparable format. The results you get from this approach may depend, however, on characteristics of the data source, independent of Word. You could also try putting the dates into a year-first format, such as the following:

{IF {MERGEFIELD Birthdate\@"YYYY/MM/DD"} > "2011/02/26" "Under 30" "Over 30"}

If the comparison still yields inconsistent results, then the only solution may be to do a mathematical conversion on the date (actually, on both Birthdate and the reference date) so that you have an actual numeric comparison. You can do this by converting the date into a Julian value, which requires a very complex merge field, like the one shown here:

{IF {QUOTE {SET a{=INT((14-{MERGEFIELD BIRTHDATE \@ M})/12)}}
{SET b{={MERGEFIELD BIRTHDATE \@ yyyy}+4800-a}}
{SET c{={MERGEFIELD BIRTHDATE \@ M}+12*a-3}}
{SET d{MERGEFIELD BIRTHDATE \@ d}}
{SET jd{=d+INT((153*c+2)/5)+365*b+INT(b/4)-INT(b/100)+INT(b/400)-32045}}
{jd \#,0}} > 2,443,056 "over 30" "under30"}

Such an approach may look intimidating, but is just the brute-force method necessary for some instances.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (356) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Proper Comparisons for Dates in Merge Fields.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Turning Off Hyphenation for Individual Words

Word does a semi-decent job when it comes to automatically hyphenating your documents. It even lets you exclude certain ...

Discover More

Understanding Grayscale Images

Word allows you to easily add images to your documents. For documents intended for monochrome printers, grayscale images ...

Discover More

Adjusting a Range's Starting Point

Select a range of cells, and one of those cells will always be the starting point for the range. This tip explains how to ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (menu)

Data Source File in Word 2003

The security features introduced in Word 2003 resulted in a change in the dialog boxes you see when opening mail-merge ...

Discover More

Conditional Processing During a Mail Merge

The Mail Merge capabilities can appear limited at first glance. One thing that is often overlooked (and which makes the ...

Discover More

Printing Portions of Mail Merged Documents

When you use a data source to create a bunch of documents in a mail merge, you might not want to print all the documents ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is six minus 4?

2019-03-21 13:07:58

Alvaro Hernandez

After thinking for no more than 20 minutes, I came up with a simpler solution (that seems to work for my specific case , but I haven´t either thought carefully about it or tested it at length).
I do know I´m oversimplifying leap years and months that don´t have exactly 30 days.
It's like this:

{IF {QUOTE {SET a{={MERGEFIELD DATE \@ yyyy}*365+{MERGEFIELD DATE \@ MM}*30+{MERGEFIELD DATE \@ d} {a \# 0}} > 736866 "a" "b"}

The comparison should be done with the desired date, e.g., 09/26/2018 (2018*365+09*30+26 = 736866).
Any thoughts?
Thanks.


2019-03-21 08:06:57

Alvaro Hernandez

Dear Mr. Wyatt,
Thank you for your tips. They are really helpful.
However, as far as this tip goes, I´d like to suggest another review because it seems to me (and let me point out I´m not a proficient VBA programmer) that it may be in error. For instance, the year of the comparison date in the example changes from 1981 to 2011. Which is the correct one? In addition, can you please clarify whether the basis date for comparison (2,443,056) was obtained using the formulas you used for jd (cause I was not able to reproduce such result using either year). And last, but not the least, I tried using your tip and it did not work for me (assuming I've implemented it correctly).
Thank you for your time and great tips (which I often come across).


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.