?
Solved

Date conversion in sql server 2012

Posted on 2016-11-18
6
Medium Priority
?
56 Views
Last Modified: 2016-12-27
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
[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
6 Comments
 
LVL 51

Expert Comment

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

Author Comment

by:rm103
ID: 41893235
yes
0
 
LVL 52

Expert Comment

by:_agx_
ID: 41893300
What was the result?

>> The date column is nvarchar(23),
Dates should be stored as dates, not strings.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 66

Expert Comment

by:Jim Horn
ID: 41893344
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:
Scott Pletcher earned 2000 total points
ID: 41893454
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
ID: 41893811
This worked perfectly.

Thanks!!
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

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
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

752 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