NetAdminTools.com
 
SignalQ Sites:
NetAdminTools - Coprolite - NoNIC - SpotBridge - NAW
RoboCoop - AreWeDown - SolarPower - SysAdminTools
Xfig - Gold Loaf - GeekPapa - FixGMC - MCJ - FixRambler
Categories:
GNU/Linux | Homebrew designs | Perl | Administration | Backup/Recovery | Bugs/Fixes | Certification | Database | Email | File/Print | Hardware | Information Grab Bag | Interoperability | GNU/Linux ABCs | Monitoring | Name Resolution | Network Services | Networking | Remote Control | Security | Desktop | Web | BSD | Solaris | GIAGD | REALbasic

Last 30 Days | Last 60 Days | Last 90 Days | All Articles | RSS | Hail Support


Categories:
·GNU/Linux
·Homebrew designs
·Perl
·Administration
·Backup/Recovery
·Bugs/Fixes
·Certification
·Database
·Email
·File/Print
·Hardware
·Information Grab Bag
·Interoperability
·GNU/Linux ABCs
·Monitoring
·Name Resolution
·Network Services
·Networking
·Remote Control
·Security
·Desktop
·Web
·BSD
·Solaris
·GIAGD
·REALbasic
·All Categories


Migrating dates to MySQL format
Topic: Perl   Posted:2002-07-03
Printer Friendly: Print

spacerspacer

If you are used to entering dates in the format DDMMYY, or have existing databases in this format, a quick perl script can migrate these for you. MySQL uses the format of YYYYMMDD. Actually, this makes a lot of sense when sorting left to right. The format for Excel spreadsheets (at least default) is this way, so if you export from a csv file generated in Excel, and import to MySQL, you will have a problem as well. Say we have a file called dt that contains:

07/04/01
02/05/03
4/4/02
5/09/01

All we need is a perl script:

while(<>){
s/(^\d*\/\d*)\/(\d\d)$/20$2\/$1/;
print;
}

If we call the perlscript dc.pl, then:

$perl dc.pl < dt
2001/07/04
2003/02/05
2002/4/4
2001/5/09

Presto, chango, we have dates we can use in MySQL. A little explanation for those of you not used to perl or regular expressions. And for those of you with moderate skills, a refresher may be useful. :) The s at the beginning is the Substitution Operator. The general form of this command is s///. The stuff that matches in the first set of slashes gets replaced with stuff in the second set of slashes. The stuff in the parentheses is put into the variables $1 and $2. So, the parentheses in the first half aren't actually matched. \\d is a digit. \\/ is a literal / for the date, since you don't want to make the command think it is the end of the match half of the substitute command. * matches any number of the previous token. so \\d* is any number of digits. ^ matches at the beginning, and $ matches at the end. So, in english, match strings such that the beginning is one or more digits and the end is one or more digits. There will be three sets of digits separated by a /. Put a 20 in front of the last set of digits, and tack on a slash and the other set of digits. Anyway, I hope this helps. I know I usually have to dig out examples each time I write a regular expression like this. Quite powerful. Quite cryptic.

-Agatha Codrust





Please read our Terms of Use
Microsoft, Windows, Windows XP, Windows 2003, Windows 2000, and NT are either trademarks or registered trademarks of Microsoft Corporation. NetAdminTools.com is not affiliated with Microsoft Corporation. Linux is a registered trademark of Linus Torvalds, and refers to the Linux kernel. The operating system of most distributions that contain the Linux kernel is GNU/Linux. All logos and trademarks in this site are property of their respective owner. Copyright 1997-2008 NetAdminTools.com

Created by:
MCJ
MCJ CMS