Solved

Date convertion

Posted on 2013-05-15
5
357 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 66

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

621 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