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

x
?
Solved

Date conversion in sql server 2012

Posted on 2016-11-18
6
Medium Priority
?
65 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 52

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 70

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

598 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