Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

change date format

Posted on 2014-01-08
3
Medium Priority
?
430 Views
Last Modified: 2014-01-09
I need to change the date format of a char(10) field to char(10) style 110. What is the most efficient way. This is sql 2005. Since the dates are consistent I suppose that I could use substring but is there a better way?

ex.05122008  change to  05-12-2008
0
Comment
Question by:qube09
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 39766496
you would normally need a double conversion ... first from string to date, and then back.
http://www.experts-exchange.com/Database/Miscellaneous/A_1499-DATE-and-TIME-don%27t-be-scared-and-do-it-right-the-first-time.html

the issue is that the format ddmmyyyy is not a "known" style for the simple convert...
so, you have to first "rework" a bi the input string for this.

hence, you could as well "skip" the double conversion, and just do string operations:
update yourtable
  set yourfield = left(yourfield,2) + '-' + substring(yourfield, 3,2) + '-' + right(yourfield,4)
where yourfield like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'

Open in new window


hope this helps
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 39766535
UPDATE dbo.tablename
SET date_column = STUFF(STUFF(date_column, 3, 0, '-'), 6, 0, '-')


For example:

SELECT date_column, STUFF(STUFF(date_column, 3, 0, '-'), 6, 0, '-')
FROM (
    SELECT '05122008' AS date_column
) AS test_data


Btw, odd format, since of course it won't sort correctly like that.
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 39767848
Why are you using char to store a date?

:)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question