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

Untitled

By: a guest on Jun 16th, 2012  |  syntax: None  |  size: 0.90 KB  |  hits: 15  |  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. SQL Server: Date conversion problem?
  2. 231280
  3. 121280
  4. 131185
  5. ...
  6.        
  7. Set DateFormat DMY
  8. GO
  9. Select Cast(Stuff(Stuff(SomeValue, 3, 0, '-'), 6, 0, '-') As datetime)
  10. From MyData
  11.        
  12. select CAST(
  13.             CASE WHEN CAST(SUBSTRING('231280', 5, 2) AS INT) >11
  14.                     THEN '19'+SUBSTRING('231280', 5, 2)
  15.                     else '20'+SUBSTRING('231280', 5, 2)
  16.                 END
  17.         +'-'+SUBSTRING('231280', 3, 2)+'-'+SUBSTRING('231280', 1, 2) as datetime)
  18.        
  19. declare @YourTable table (YourColumn varchar(50))
  20. insert @YourTable
  21.           select '231280'
  22. union all select '121280'
  23. union all select '131185'
  24.  
  25. select  convert(datetime, substring(YourColumn,1,2) + '/' +
  26.     substring(YourColumn,3,2) + '/' + substring(YourColumn,5,2), 3)
  27. from    @YourTable
  28.        
  29. DECLARE @field char(6)
  30. SET @field = '231280'
  31. select convert(datetime, right(@field, 2) +   substring(@field, 3, 2) + left(@field, 2)  , 12)