Problem Solution Discussion Converting Two-Digit Year Values to Four-Digit Form

For t his m et hod, t he hash act s as a cache, so t hat you run a lookup query for any given value only once, no m at t er how m any t im es it occurs in t he input . For dat aset s t hat have a reasonable num ber of repeat ed values, t his approach avoids issuing a separat e query for every single value, while requiring an ent ry in t he hash only for each unique value. I t t hus st ands bet ween t he ot her t wo approaches in t erm s of t he t radeoff bet ween dat abase t raffic and program m em ory requirem ent s for t he hash. Not e t hat t he hash is used in a som ewhat different m anner for t his m et hod t han for t he previous m et hod. Previously, t he exist ence of t he input value as a key in t he hash det erm ined t he validit y of t he value, and t he value associat ed wit h t he hash key was irrelevant . For t he hash-as-cache m et hod, t he m eaning of key exist ence in t he hash changes from it s valid t o it s been t est ed before. For each key, t he value associat ed wit h it indicat es whet her t he input value is present in t he lookup t able. I f you st ore as keys only t hose values t hat are found t o be in t he lookup t able, youll issue a query for each inst ance of an invalid value in t he input dat aset , which is inefficient .

10.30 Converting Two-Digit Year Values to Four-Digit Form

10.30.1 Problem

You need t o convert years in dat e values from t wo digit s t o four digit s.

10.30.2 Solution

Let MySQL do t his for you. I f MySQLs conversion rules arent appropriat e, perform t he operat ion yourself.

10.30.3 Discussion

Two- digit year values are a problem because t he cent ury is not explicit in t he dat a values. I f you know t he range of years spanned by your input , you can add t he cent ury wit hout am biguit y. Ot herwise, you can only guess. For exam ple, t he dat e 2 10 69 probably would be int erpret ed by m ost people in t he U.S. as as Oct ober 2, 1969. But if it represent s Mahat m a Gandhis birt h dat e, t he year act ually is 1869. One way t o convert years t o four digit s is t o let MySQL do it . I f you st ore a dat e cont aining a t w o- digit year, MySQL aut om at ically convert s it t o four- digit form . MySQL uses a t ransit ion point of 1970; it int erpret s values from 00 t o 69 as t he years 2000 t o 2069, and values from 70 t o 99 as t he years 1970 t o 1999. These rules are appropriat e for year values in t he range from 1970 t o 2069. I f your values lie out side t his range, you should add t he proper cent ury yourself before st oring t hem int o MySQL. To use a different t ransit ion point , convert years t o four- digit form yourself. A general purpose rout ine t o convert t wo- digit years t o four digit s and t o allow an arbit rary t ransit ion point can be writ t en like t his: sub yy_to_ccyy { my year, transition_point = _; transition_point = 70 unless defined transition_point; year += year = transition_point ? 1900 : 2000 if year 100; return year; } The funct ion uses MySQLs t ransit ion point 70 by default . An opt ional second argum ent m ay be given t o provide a different t ransit ion point . yy_to_ccyy also m akes sure t he year act ually needs convert ing is less t han 100 before m odifying it . That w ay you can pass year values t hat do or dont include t he cent ury wit hout checking first . Som e sam ple invocat ions using t he default t ransit ion point have t he following result s: val = yy_to_ccyy 60; returns 2060 val = yy_to_ccyy 1960; returns 1960 no conversion done But suppose you want t o convert year values as follows, using a t ransit ion point of 50: 00 .. 49 - 2000 .. 2049 50 .. 99 - 1950 .. 1999 To do t his, pass an explicit t ransit ion point argum ent t o yy_to_ccyy : val = yy_to_ccyy 60, 50; returns 1960 val = yy_to_ccyy 1960, 50; returns 1960 no conversion done The yy_to_ccyy funct ion is one of t hose included in t he Cookbook_Ut ils.pm library file.

10.31 Performing Validity Checking on Date or Time Subparts