Conversion

How can I get

"2001-07-02 00:00:00.000" to "20010702"


August 21st, 2015 12:09pm

Hi kalsubu

Check this

SELECT REPLACE(CONVERT(VARCHAR(10), '2001-07-02 00:00:00.000', 112),'-','')

Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 12:17pm

To add on to Milan's solution, formatting data for display purposes is a task best done in the presentation layer.  In .NET, you can use a format string:

someDateTimeValue.ToString("yyyyMMdd")



August 21st, 2015 12:28pm

One more method

DECLARE @dt AS DATETIME2 ='2001-07-02 00:00:00.000'

SELECT CONVERT(CHAR(15),@dt,112)

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 5:39am

DECLARE @dt AS DATETIME2 ='2001-07-02 00:00:00.000'

SELECT CONVERT(VARCHAR(10),@dt,112)
Not sure why did we use CHAR(15) (5 extra char) in the previous response. That's why I changed my mind.
August 25th, 2015 12:31am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics