Improve company productivity with a Business Account.Sign Up

x
?
Solved

Parsing an ugly time string into clean datetime value

Posted on 2006-07-07
4
Medium Priority
?
389 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
2 Comments
 
LVL 8

Accepted Solution

by:
Shakti109 earned 1000 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 1000 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

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Today, unlike web development, the mobile landscape is complex enough for a software engineer and Android is posing more challenging environment thanks to its fragmentation issues on hardware and software fronts.
This article presents several of my favorite code snippets.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Simple Linear Regression

579 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