Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

String to date conversion issue Expand / Collapse
Posted Sunday, August 25, 2013 4:53 PM


Group: General Forum Members
Last Login: Tuesday, February 2, 2016 2:45 PM
Points: 193, Visits: 766
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
Post #1488265
Posted Sunday, August 25, 2013 8:51 PM



Group: General Forum Members
Last Login: Today @ 12:27 PM
Points: 39,659, Visits: 36,786
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Favorite Quotes:
"Has anyone ever told you that a query you have written runs too fast?" - Dwain Camps - 6 Mar 2014

Helpful Links:
How to post code problems
How to post performance problems
Post #1488275
Posted Monday, August 26, 2013 10:05 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 1:42 PM
Points: 3,431, Visits: 5,389
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)

"If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them." James Blackburn, in closing argument in the "Fatal Vision" murders trial
Post #1488441
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse