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
  • 704,970
  • 168
  • 1,303
  • 1,425
dopplesoldner
  • 8,015
  • 12
  • 38
  • 54

3 Answers3

12
select convert (datetime,  Stuff(Stuff('311012',5,0,'.'),3,0,'.'), 4)
bummi
  • 26,839
  • 13
  • 60
  • 97
  • "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,013
  • 4
  • 24
  • 41
-2
select CAST('121031' AS datetime)  as d -- For YYMMDD
Ranjith J
  • 107
  • 1
  • 6