6

I am trying to convert a date from DDMMYY format to datetime in SQL Server.

I am using the convert command as follows

select convert (datetime, '311012', <style>)

I have tried looking on msdn for the supported styles but haven't found an exact match.

Help will be much appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
dopplesoldner
  • 8,891
  • 12
  • 44
  • 55

3 Answers3

12
select convert (datetime,  Stuff(Stuff('311012',5,0,'.'),3,0,'.'), 4)
bummi
  • 27,123
  • 14
  • 62
  • 101
  • "Stuff" inserst a string in another sting. In this example, "Stuff" is used twice to insert two periods (at index 5 (fist) and 3 (last)). Thus transforming "311012" to "31.10.12" which is a datetime format (4) that convert can understand. – Andreas Jan 18 '17 at 12:21
0

Try like this :-

declare  @val1 varchar(30)
select @val1=SUBSTRING('311012',1 ,2)+'/'+SUBSTRING('311012',3 ,2)+'/'+'20'+SUBSTRING('311012',5 ,2)
SELECT CONVERT(datetime,@val1,103)
Pranav
  • 8,563
  • 4
  • 26
  • 42
-2
select CAST('121031' AS datetime)  as d -- For YYMMDD
Ranjith J
  • 107
  • 1
  • 6