Guest User

aredna mayan date conversion bonus 1

a guest
Jan 16th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. declare @i varchar(999) = '3 1 1 1970 20 7 1988 12 12 2012';
  2.  
  3. ;with parseinput as
  4. (
  5. select n = cast(substring(@i,1,charindex(' ',@i)-1) as int)*3,
  6. i = cast(substring(@i,charindex(' ',@i)+1,999)+' ' as varchar(100)),
  7. x = 0,
  8. t = 'x'
  9. union all
  10. select n = n-1,
  11. i = cast(substring(i,charindex(' ',i)+1,999) as varchar(100)),
  12. x = cast(substring(i,1,charindex(' ',i)-1) as int),
  13. t = case n%3 when 0 then 'd' when 2 then 'm' else 'y' end
  14. from parseinput
  15. where n > 0
  16. )
  17. ,pivotdates as
  18. (
  19. select n = n/3,
  20. d = max(case when t = 'd' then x else 0 end),
  21. m = max(case when t = 'm' then x else 0 end),
  22. y = max(case when t = 'y' then x else 0 end)
  23. from parseinput
  24. where t <> 'x'
  25. group by n/3
  26. )
  27. ,convertdates as
  28. (
  29. select n, d, m, y
  30. from pivotdates
  31. )
  32. ,leapyears as
  33. (
  34. select 1972 as yr
  35. union all
  36. select yr+case when (yr+4) % 100 = 0 and (yr+4) % 400 <> 0 then 8 else 4 end
  37. from leapyears
  38. where yr < 100000
  39. )
  40. ,datediffs as
  41. (
  42. select n,
  43. midx = 1856304
  44. + (y-1970)*365
  45. + case m when 1 then 0 when 5 then 120 when 9 then 243
  46. when 2 then 31 when 6 then 151 when 10 then 273
  47. when 3 then 59 when 7 then 181 when 11 then 304
  48. when 4 then 90 when 8 then 212 when 12 then 334
  49. end
  50. + (d)
  51. + ( select count(*) from leapyears where yr<y or (yr=y and m>2) )
  52. from pivotdates
  53. )
  54. ,convertmayan as
  55. (
  56. select n,
  57. baktun = cast(midx/20/20/18/20 as varchar(9)),
  58. katun = cast(midx/20/18/20 % 20 as varchar(9)),
  59. tun = cast(midx/20/18 % 20 as varchar(9)),
  60. winal = cast(midx/20 % 18 as varchar(9)),
  61. kin = cast(midx % 20 as varchar(9))
  62. from datediffs
  63. )
  64. select baktun+'.'+katun+'.'+tun+'.'+winal+'.'+kin
  65. from convertmayan
  66. order by n desc
  67. option (maxrecursion 32000)
Advertisement
Add Comment
Please, Sign In to add comment