Results 1 to 9 of 9

Thread: Converting Varchar to Date time

  1. #1
    Join Date
    May 2009
    Posts
    9

    Converting Varchar to Date time

    Hi,

    I am having problems converting some dates on my database.

    I have a table which contains submissions from our partners in XML. One of the fields submitted is date of birth for the customer. Regardless to the fact that all providers are told how this should be submitted some still can't do this correctly. Hence I end up with a lot of varchars which are dates, but in all sorts of formats.

    Is there and easy way to handle this conversion?

    Thanks.

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    Can't think about any easier way than enforce format in frontend.

  3. #3
    Join Date
    May 2009
    Posts
    9
    We alredy do, but experience proven that a lot of people are not capable of doing what they are told... :/

    Thanks for reply.

  4. #4
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    You can write a function to convert different formats to one common format.

  5. #5
    Join Date
    May 2009
    Posts
    9
    Tried that. The issue is my inability of determining the date format before I run the function/query. This would mean that I will need a function which will cover all possibilities.

  6. #6
    Join Date
    Apr 2009
    Posts
    86
    wujtehacjusz, From what you describe (the date can be in ANY format), I don't think there is any way to determine the actual date.

    For example, if some one enters 03 06 09, it could be:
    Mar 6 2009
    Mar 9 2006
    Jun 3 2009
    Jun 9 2003
    Sep 3 2006
    Sep 6 2003

    Since you have no idea what order the Year, Month and Day are in, all of these values could be valid (and you would have a 5 in 6 chance of being wrong).

    About the only way I can think of to even attempt this is to take all the values from one source and put them into 3 separate columns. Then do some queries and see if:

    A column could be a Year
    If it is a 4 digit number.
    All the columns have a 2 digit value that is 1 (or very few) number(s) that match or are around the current year.
    (sorry, I just re-read the original post and the date is birth date. This would make it very difficult to determine if a 2 digit value is a Year).

    A column is a day if contains values greater than 12 (assuming it is not a year).

    A column could be a month if it only contains values between 1 and 12.

    NOTE: there may be other factors to check that could help determine the Year, Month, and Day positions.

    Alternately, you may be able to generate a list of the various sources and the format that each one uses. If each source is consistent in the format they supply, you might be able to set up special processes based on the supplier to parse out the correct date.

  7. #7
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    YOu could write a function or use case function to update the data.

  8. #8
    Join Date
    May 2009
    Posts
    9
    Problem I have is that unsuccesful conversion will throw an excepton which will prevent the execution. Sadly I can't use try catch in functions...

    Thanks for reply.

  9. #9
    Join Date
    Jun 2004
    Location
    Atlanta and Manhattan
    Posts
    607

    Could be a major challenge ...

    If date can be in "any format," you've got a real challenge... Can you at least assume that the format is consistent for all dates from a given provider? If so, and if you can derive a rule for each provider's format, you could try to apply the rule to the respective providers submissions - perhaps you could group by provider / create provider views / pump into a staging table, etc., and then try to get all dates to the same format before doing further conversions.

    You could even do something like this with expressions in Reporting Services, but you really need to get the existing, underlying data consistent - and then apply (and enforce) new rules going forward (mechanically - not just through requests, etc.)

    Just some ideas ...

    Bill

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •