将SQL Server DateTime对象转换为BIGINT(.Net ticks)

前端之家收集整理的这篇文章主要介绍了将SQL Server DateTime对象转换为BIGINT(.Net ticks)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要将DateTime类型值转换为.Net ticks格式的BIGINT类型(0001年1月1日凌晨12:00之后已经过去的100纳秒间隔).

转换应该在sql Server 2008中使用T-SQL查询执行

例如:

  1. DateTime value - 12/09/2011 00:00:00

将转换为:

  1. BIGINT value - 634513824000000000

解决方法

我找到了可以协助的CodeProject文章Convert DateTime To .NET Ticks Using T-SQL

我附上上面的文章sql函数(我希望这样可以吗?因为它需要注册.)

  1. CREATE FUNCTION [dbo].[MonthToDays365] (@month int)
  2. RETURNS int
  3. WITH SCHEMABINDING
  4. AS
  5. -- converts the given month (0-12) to the corresponding number of days into the year (by end of month)
  6. -- this function is for non-leap years
  7. BEGIN
  8. RETURN
  9. CASE @month
  10. WHEN 0 THEN 0
  11. WHEN 1 THEN 31
  12. WHEN 2 THEN 59
  13. WHEN 3 THEN 90
  14. WHEN 4 THEN 120
  15. WHEN 5 THEN 151
  16. WHEN 6 THEN 181
  17. WHEN 7 THEN 212
  18. WHEN 8 THEN 243
  19. WHEN 9 THEN 273
  20. WHEN 10 THEN 304
  21. WHEN 11 THEN 334
  22. WHEN 12 THEN 365
  23. ELSE 0
  24. END
  25. END
  26.  
  27. GO
  28.  
  29. CREATE FUNCTION [dbo].[MonthToDays366] (@month int)
  30. RETURNS int
  31. WITH SCHEMABINDING
  32. AS
  33. -- converts the given month (0-12) to the corresponding number of days into the year (by end of month)
  34. -- this function is for leap years
  35. BEGIN
  36. RETURN
  37. CASE @month
  38. WHEN 0 THEN 0
  39. WHEN 1 THEN 31
  40. WHEN 2 THEN 60
  41. WHEN 3 THEN 91
  42. WHEN 4 THEN 121
  43. WHEN 5 THEN 152
  44. WHEN 6 THEN 182
  45. WHEN 7 THEN 213
  46. WHEN 8 THEN 244
  47. WHEN 9 THEN 274
  48. WHEN 10 THEN 305
  49. WHEN 11 THEN 335
  50. WHEN 12 THEN 366
  51. ELSE 0
  52. END
  53. END
  54.  
  55. GO
  56.  
  57. CREATE FUNCTION [dbo].[MonthToDays] (@year int,@month int)
  58. RETURNS int
  59. WITH SCHEMABINDING
  60. AS
  61. -- converts the given month (0-12) to the corresponding number of days into the year (by end of month)
  62. -- this function is for non-leap years
  63. BEGIN
  64. RETURN
  65. -- determine whether the given year is a leap year
  66. CASE
  67. WHEN (@year % 4 = 0) and ((@year % 100 != 0) or ((@year % 100 = 0) and (@year % 400 = 0))) THEN dbo.MonthToDays366(@month)
  68. ELSE dbo.MonthToDays365(@month)
  69. END
  70. END
  71.  
  72. GO
  73.  
  74. CREATE FUNCTION [dbo].[TimeToTicks] (@hour int,@minute int,@second int)
  75. RETURNS bigint
  76. WITH SCHEMABINDING
  77. AS
  78. -- converts the given hour/minute/second to the corresponding ticks
  79. BEGIN
  80. RETURN (((@hour * 3600) + CONVERT(bigint,@minute) * 60) + CONVERT(bigint,@second)) * 10000000
  81. END
  82.  
  83. GO
  84.  
  85. CREATE FUNCTION [dbo].[DateToTicks] (@year int,@month int,@day int)
  86. RETURNS bigint
  87. WITH SCHEMABINDING
  88. AS
  89. -- converts the given year/month/day to the corresponding ticks
  90. BEGIN
  91. RETURN CONVERT(bigint,(((((((@year - 1) * 365) + ((@year - 1) / 4)) - ((@year - 1) / 100)) + ((@year - 1) / 400)) + dbo.MonthToDays(@year,@month - 1)) + @day) - 1) * 864000000000;
  92. END
  93.  
  94. GO
  95.  
  96. CREATE FUNCTION [dbo].[DateTimeToTicks] (@d datetime)
  97. RETURNS bigint
  98. WITH SCHEMABINDING
  99. AS
  100. -- converts the given datetime to .NET-compatible ticks
  101. -- see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatetimeclasstickstopic.asp
  102. BEGIN
  103. RETURN
  104. dbo.DateToTicks(DATEPART(yyyy,@d),DATEPART(mm,DATEPART(dd,@d)) +
  105. dbo.TimeToTicks(DATEPART(hh,DATEPART(mi,DATEPART(ss,@d)) +
  106. (CONVERT(bigint,DATEPART(ms,@d)) * CONVERT(bigint,10000));
  107. END
  108.  
  109. GO

猜你在找的MsSQL相关文章