Solved

Date conversion in sql server 2012

Posted on 2016-11-18
6
42 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
6 Comments
 
LVL 48

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 65

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 500 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

856 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