Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Date convertion

Posted on 2013-05-15
5
Medium Priority
?
363 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 500 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 500 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 500 total points
ID: 39171244
if SQL server 2012 then


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

Assisted Solution

by:sachitjain
sachitjain earned 500 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 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