Solved

Date convertion

Posted on 2013-05-15
5
352 Views
Last Modified: 2013-05-22
How can I convert this date format to YYYYMMDD?

2013-04-02 00:00:00.000

to

YYYYMMDD
20130402
0
Comment
Question by:VBdotnet2005
[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
5 Comments
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 125 total points
ID: 39168876
For some reason this works in my SSMS
Declare @dt datetime = '2013-04-02 00:00:00.000'
SELECT REPLACE(CONVERT(date, @dt, 112), '-', '')

Open in new window

and this doesn't
SELECT REPLACE(CONVERT(date, '2013-04-02 00:00:00.000', 112), '-', '')

Open in new window

0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 125 total points
ID: 39168915
> For some reason this works in my SSMS
You are converting it to 'Date', thats why it doesn't strip of the '-'s,

SELECT CONVERT(varchar, cast('2013-04-02 00:00:00.000' as datetime), 112)
0
 
LVL 37

Expert Comment

by:ValentinoV
ID: 39170449
In case you expect your output to be string, I'd suggest this:

Declare @dt datetime = '2013-04-02 00:00:00.000'
SELECT CONVERT(char(8), @dt, 112)

Or perhaps you're expecting a number? Then:

Declare @dt datetime = '2013-04-02 00:00:00.000'
SELECT CONVERT(int, CONVERT(char(8), @dt, 112))
0
 
LVL 21

Assisted Solution

by:Alpesh Patel
Alpesh Patel earned 125 total points
ID: 39171244
if SQL server 2012 then


Select Format(GETDATE(),'yyyyMMdd')
0
 
LVL 12

Assisted Solution

by:sachitjain
sachitjain earned 125 total points
ID: 39186434
SQL Server any version, this would surely work

declare @dt datetime
set @dt = getdate()
select convert(varchar(4), year(@dt)) + right('0' + convert(varchar(2), month(@dt)), 2) + right('0' + convert(varchar(2), day(@dt)), 2)
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.

734 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