SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


String to date conversion issue


String to date conversion issue

Author
Message
simflex-897410
simflex-897410
Old Hand
Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)

Group: General Forum Members
Points: 311 Visits: 767
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
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86904 Visits: 41105
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
ScottPletcher
ScottPletcher
SSCertifiable
SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)

Group: General Forum Members
Points: 7945 Visits: 7160
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)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: 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.[/size]
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search