Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.05 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to convert mmm d yyyy HH:MM[AM] to datetime in SQL? [closed]
  2. convert(datetime, 'Aug  4 2011 12:00AM')
  3.        
  4. select convert(datetime, 'Aug  4 2011 12:00AM') as ConvertResult
  5.  
  6. ConvertResult
  7. -------------
  8. 2011-08-04 00:00:00.000
  9.        
  10. select convert(datetime, 'Aug  4 2011 12:00AM', 100) as ConvertResult
  11.  
  12. ConvertResult
  13. -------------
  14. 2011-08-04 00:00:00.000
  15.        
  16. convert(datetime, 'Aug  4 2011 12:00AM', 120)
  17.        
  18. declare @datestring varchar(30);
  19. set @datestring = 'Aug  4 2011 12:04AM';
  20.  
  21. select convert(datetime,
  22.     substring(@datestring, 8,4) + '-' +
  23.     case lower(left(@datestring, 3))
  24.         when 'jan' then '01'
  25.         when 'feb' then '02'
  26.         when 'mar' then '03'
  27.         when 'apr' then '04'
  28.         when 'may' then '05'
  29.         when 'jun' then '06'
  30.         when 'jul' then '07'
  31.         when 'aug' then '08'
  32.         when 'sep' then '09'
  33.         when 'oct' then '10'
  34.         when 'nov' then '11'
  35.         when 'dec' then '12'
  36.     end + '-' +
  37.     right('00' + ltrim(rtrim(substring(@datestring, 5, 2))), 2) + ' ' +
  38.     substring(@datestring, 13,50)
  39. ) as Converted;