Solved

Date conversion in sql server 2012

Posted on 2016-11-18
6
23 Views
Last Modified: 2016-11-18
Hello: I need to convert date stored in a sql server table/column as 20161116 13:49:43.81
to dd/mm/yyyy that can be sorted in Excel. The date column is nvarchar(23), null and encompass in a select statement.
Any feedback appreciated.  

Thanks,

Rm103
0
Comment
Question by:rm103
6 Comments
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
You should be able to export to Excel as is. Did you even tried?
0
 

Author Comment

by:rm103
Comment Utility
yes
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
What was the result?

>> The date column is nvarchar(23),
Dates should be stored as dates, not strings.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
This works on my 2008R2 box to convert to a valid datetime
Declare @dt nvarchar(23) = '20161116 13:49:43.81'
SELECT CAST(LEFT(@dt, 8) + ' ' + RIGHT(@dt, 11) as datetime) 

Open in new window

To convert to a varchar formatted as dd/mm/yyyy in SQL Server
Declare @dt nvarchar(23) = '20161116 13:49:43.81'
SELECT convert(varchar, CAST(LEFT(@dt, 8) as DATE) ,103)

Open in new window

See SQL Server Date Styles (formats) using CONVERT() for handy conversion functions.

.. or you can just pass the value as a date and let Excel format it however you wish.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
Since it's already a string, I would leave it a string, no need to risk a conversion to date:

SELECT
    date_column AS original_date_column,
    STUFF(LEFT(date_column, 4), 1, 0, SUBSTRING(date_column, 7, 2) + '/' + SUBSTRING(date_column, 5, 2) + '/') AS date_ddmmyyyy
FROM (
    VALUES('20161116 13:49:43.813')
) AS test_data(date_column)
0
 

Author Closing Comment

by:rm103
Comment Utility
This worked perfectly.

Thanks!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now