Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

String to date conversion issue Expand / Collapse
Author
Message
Posted Sunday, August 25, 2013 4:53 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Saturday, March 15, 2014 7:36 PM
Points: 182, Visits: 705
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


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 10:28 PM
Points: 35,215, Visits: 31,666
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

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

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 3:14 PM
Points: 2,098, Visits: 3,155
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)

"We came in spastic, Like tameless horses /
We left in plastic, As numbered corpses / ...
Remember Charlie, Remember Baker /
They left their childhood On every acre /
And who was wrong? And who was right? /
It didn't matter in the thick of the fight." : the inimitable Mr. Billy Joel, about the Vietnam War
Post #1488441
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse