Solved

Parsing an ugly time string into clean datetime value

Posted on 2006-07-07
4
374 Views
Last Modified: 2010-04-17
I am working on a data conversion for a client and I need to convert a string column in an access database to a datetime column in a sql database.  We are not using the date portion of this field so it doesnt matter, but i need the time to be accurate.  I am hoping that someone will know of a nice, comprehensive routine to convert these ugly strings to nice times.  I'll give some examples of the data that I have to convert:
"8","900","930","915AM","1030AM","9.15AM","9:30am0","9:00am,","9;00--","9;00AM4","90045AM" (this one may just need to be dropped), "9 00 am".   The DateTime.Parse routine in .NET can handle some of these cases but definitely not all.

Please help me so I dont have to spend hours and hours coming up with a routine to deal with all these different cases.

Thanks in advance.
0
Comment
Question by:dougschultz
4 Comments
 
LVL 8

Accepted Solution

by:
Shakti109 earned 250 total points
ID: 17058636

I do not know of any single routine that will do what you are looking for. You essentially need to parse the input and look for identifiers.

I would reccomend using regular expressions.

An example of a simple pattern (that would "detect" one of your patterns)

Dim rx as new regex ("[0-9]{3}")   : This will find occurances of any series of 3 numbers (0-9).

OR

Dim rx as new regex ("[0-9]{1}.[0-9]{2}")  : This will find occurances of the pattern #.##.

Regular expressions are quite powerful, fast, and once you understand the syntax you can create the patterns for them quite quickly.

Some useful links explaining their use are :

http://visualbasic.about.com/gi/dynamic/offsite.htm?site=http://aspnet.4guysfromrolla.com/articles/022603%2D1.aspx

http://visualbasic.about.com/od/usingvbnet/l/blregexa.htm?terms=microsoft+e+book

There are also programs you can download (some free, some not) that will even help you create the regular expressions, test them with input, and in some cases even generate the code behind them.

One such program is :
http://www.sellsbrothers.com/tools/#regexd


While it will be a little bit of a learning curve, learning to use regular expressions will be a very powerful tool for your programming skill-set.


0
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 250 total points
ID: 17065069
Shakti109 is right.  Regular Expressions (regexp) are the way to go.

However, I would revise his comments a bit

Dim rx as new regex ("[0-9]{3}")               : Use to find a sequence of 3 digits (0-9)

Dim rx as new regex ("[0-9]{1}.[0-9]{2}")  : Use to find a single digit, followed by something, followed by 2 digits

  The difference in wording is very specific.  The first pattern can only be used to match 3 consecutive digits.
The second is different because the period ('.') is used to match (almost) any charater.  So, this pattern will
match either: 1.00 or 1,23 or 9x11.

  As far as your request "...so I dont have to spend hours and hours coming up with a routine..." this may not
be a good answer.  If you are unfamiliar with regular expressions, you could very well spend "hours and hours"
learning about the nuances.

  You might consider using an iterative approach like:

- Export the access data to a file format that can be processed by the language wth which you want to work
  (this might be optional, if the language is able to read the Access database directly)
- Process the date/time field looking for common patterns
  For example, how many (what percentage) of the data can be handled by a simple test like
  if ( value == single digit ) {
    ...
  }
- The iteration is that you add to this "simple" test in an effort to maximize coverage of the automatic
  processing of the data, and minimize the "manual" (i.e., exceptions)
- If you are really persistent, this iteration will cover all of the data patterns in the time available.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now