String to date conversion issue

  • Users of our app currently store date values in the following format:

    Mon. dd, yyyy.

    Example: Sept. 23, 2013

    We would like to convert these date values to MM/dd/yyyy, eg, 9/23/2013 and the code below does just that.

    SELECT CONVERT(VARCHAR(10), CONVERT(DATETIME, SUBSTRING('Sept. 23, 2013', 0, 4) + ' ' + SUBSTRING('Sept. 23, 2013',6,9), 107), 101)

    When we run this code, now, we get above date, for instance, formatted to 9/23/2013.

    However, the issue we have now is that when we try to update same date value, it stores a different formatted value like:

    Sep 23 2013 12:00AM

    Notice that first, Sept. is now Sep which is wrong because t and period (.) are missing.

    Simply put, when we update a date value, it needs to follow similar format as this -> Sept. 23, 2013

    Any ideas how to modify above query to produce similar result when a date value is updated?

    Thanks a lot in advance

  • simflex-897410 (8/25/2013)


    However, the issue we have now is that when we try to update same date value, it stores a different formatted value like:

    And now you understand why storing a formatted date of any kind is one of the worst database sins there is. Store all dates using one of the date/time or date datatypes and format only when consumed. If you're really pressed, create a persisted calculated column to do the formatting for you but don't store the actual data as a formatted date/time of any type.

    As a bit of a sidebar, the app should check what the user has entered to see if it can even be converted to a date and, if it can, IT should pass the date as a datetime or date datatype. If it's not a valid date, the app should alert the user without even touching the database.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • You have to separate the data format the user enters vs. what's stored in the db. It's OK in your app to have the user enter "mon. dd, yyyy" if that's what you prefer. However, you should store an actual date or datetime column in the db, not the text the user enters.

    Likewise, you might allow users to enter a SSN as ###-##-####, but you shouldn't store the dashes in the db.

    Use a computed column to add any formatting for output; persist that computed column only if you have to (you almost never need to persist the formatted data).

    Thus, you would have a computed column that would redisplay the data back to the user in the format they expect. The internal SQL storage format (which, for data/datetime data types is actually integer(s)) should be transparent, and therefore irrelevant, to the user.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply