Solved

Parsing an ugly time string into clean datetime value

Posted on 2006-07-07
4
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Starting up a Project

690 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