22

How do I convert YYYY-MM-DD (2012-08-17) to a date in SQL Server?

I don't see this format listed on the help page: http://msdn.microsoft.com/en-us/library/ms187928.aspx

5 Answers 5

28

I think style no. 111 (Japan) should work:

SELECT CONVERT(DATETIME, '2012-08-17', 111)

And if that doesn't work for some reason - you could always just strip out the dashes and then you have the totally reliable ISO-8601 format (YYYYMMDD) which works for any language and date format setting in SQL Server:

SELECT CAST(REPLACE('2012-08-17', '-', '') AS DATETIME)
5
  • 1
    is there no way of specifying the format like 'ddmmyyyy'? having a random number just seems really horrible
    – JonnyRaa
    Apr 7, 2014 at 15:50
  • I think that's for going in the opposite direction - from a date to a string. There doesnt appear to be an equivalent
    – JonnyRaa
    Apr 7, 2014 at 16:28
  • Using styles in CONVERT is only relevant when convert from a date or time to a character string. When converting a date or time to a character string styles are irrelevant. Sep 27, 2017 at 2:38
  • But why strip the dashes? Which versions of SQL Server don't support that? It works perfectly on all of the versions I have on hand, admittedly not many that old. Jul 3, 2018 at 4:55
  • 1
    @SilverbackNet: try SET LANGUAGE GERMAN; SELECT CAST('2018-06-25' AS DATETIME) and you'll see that this ends in an error - while if you use the 20180625 without the dashes, it works (regardless of language / regional settings)
    – marc_s
    Jul 3, 2018 at 5:17
6

This will do the trick:

SELECT CONVERT(char(10), GetDate(),126)
2
1

I had a similar situation. Here's what I was able to do to get a date range in a "where" clause (a modification of marc_s's answer):

where cast(replace(foo.TestDate, '-', '') as datetime) 
      between cast('20110901' as datetime) and
              cast('20510531' as datetime)

Hope that helps...

0

if you datatype is datetime of the table.col , then database store data contain two partial : 1 (date) 2 (time)

Just in display data use convert or cast.

Example:

create table #test(part varchar(10),lastTime datetime)
go

insert into #test (part ,lastTime )
values('A','2012-11-05 ')

insert into #test (part ,lastTime )
values('B','2012-11-05 10:30')


go

select * from #test 

A   2012-11-05 00:00:00.000
B   2012-11-05 10:30:00.000

select part,CONVERT (varchar,lastTime,111) from #test

A   2012/11/05
B   2012/11/05

select part,CONVERT (varchar(10),lastTime,20) from #test 

A   2012-11-05
B   2012-11-05
0

Write a function

CREATE FUNCTION dbo.SAP_TO_DATETIME(@input VARCHAR(14))
RETURNS datetime
AS BEGIN
    DECLARE @ret datetime
    DECLARE @dtStr varchar(19)

    SET @dtStr = substring(@input,1,4) + '-' +  substring(@input,5,2) + '-' + substring(@input,7,2)
                + ' ' + substring(@input,9,2) + ':' + substring(@input,11,2) + ':' + substring(@input,13,2);        

    SET @ret = COALESCE(convert(DATETIME, @dtStr, 20),null);
    RETURN @ret
END

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.