Using a user-defined type CLR UDT

Source: Internet
Author: User

??

It is not necessary for some composite types to perform paradigm decomposition, especially in the case of some unified models

??

DECLARE @i TimeBalance

SET @i = CAST(' 0/102 'asTimeBalance)

SELECT @i

SELECT @i. ToString()

SELECT @i. toTimeString()

SELECT @i. Distance()

SELECT @i. Distance2(' mi ')

??

SET @i = CAST(' 2015-1-1/2016-8-21 'asTimeBalance)

SELECT @i. ToString() string ,

@i. toTimeString() timestring ,

@i. Distance() Distance ,

@i. Distance2(' m ')distancemonth

??

??

??

?

Related information:

?

TSql vs. SQL CLR Performance Analysis

?

CLR-Integrated performance

Manipulating UDT Data

?

??

  1. <summary>
  2. Time Wallet Balance Type
  3. ///
  4. </summary>
  5. <remarks>
  6. Based on the long integer type, the structure is as follows:
  7. Start UTC seconds 4B, end UTC Seconds 4B
  8. </remarks>
  9. <see cref= "https://msdn.microsoft.com/en-us/library/ms131086.aspx"/>
  10. [Serializable]
  11. [Microsoft.SqlServer.Server.SqlUserDefinedType (Format.native)]
  12. Public struct Timebalance:inullable
  13. {
  14. ???? Public Override string ToString ()
  15. ???? {
  16. ??
  17. ???????? return string. Format ("{0}/{1}", Startsecond, Endsecond);
  18. ????}
  19. ??
  20. ???? Public string toTimeString ()
  21. ???? {
  22. ???????? return string. Format ("{0}/{1}", Utcsecondtodatetime (Startsecond). ToString ("yyyy-mm-dd HH:mm:ss"), Utcsecondtodatetime (Endsecond). ToString ("yyyy-mm-dd HH:mm:ss"));
  23. ????}
  24. ??
  25. ???? Public Static TimeBalance Parse (SqlString s)
  26. ???? {
  27. ???????? if (S.isnull)
  28. ???????????? return Null;
  29. ??
  30. ???????? TimeBalance u = new TimeBalance ();
  31. ???????? if (S.value.contains ("-"))
  32. ???????? {
  33. ???????????? U.startsecond = Datetimetoutcsecond (DateTime.Parse (S.value.split ('/') [0]));
  34. ???????????? U.endsecond = Datetimetoutcsecond (DateTime.Parse (S.value.split ('/') [1]));
  35. ????????} Else
  36. ???????? if (S.value.contains ("/"))
  37. ???????? {
  38. ???????????? U.startsecond = int. Parse (S.value.split ('/') [0]);
  39. ???????????? U.endsecond = int. Parse (S.value.split ('/') [1]);
  40. ????????}
  41. ???????? Else
  42. ???????? {
  43. ???????????? byte [] bs = bitconverter.getbytes (long. Parse (S.value));
  44. ???????????? U.startsecond = Bitconverter.toint32 (BS, 0);
  45. ???????????? U.endsecond = Bitconverter.toint32 (BS, 4);
  46. ??
  47. ????????}
  48. ???????? return u;
  49. ????}
  50. ??
  51. ??
  52. ??
  53. ??
  54. ???? Public BOOL IsNull
  55. ???? {
  56. ???????? Get
  57. ???????? {
  58. ???????????? //Place code here
  59. ???????????? return _null;
  60. ????????}
  61. ????}
  62. ??
  63. ???? Public Static TimeBalance Null
  64. ???? {
  65. ???????? Get
  66. ???????? {
  67. ???????????? TimeBalance h = new TimeBalance ();
  68. ???????????? H._null = true;
  69. ???????????? return H;
  70. ????????}
  71. ????}
  72. ??
  73. ??
  74. ???? Public Double Distance2 (SqlString type)
  75. ???? {
  76. ???????? Switch (Type. Value.tolower ())
  77. ???????? {
  78. ???????????? Case "y":
  79. ???????????????? return Utcsecondtodatetime (Endsecond). Year-utcsecondtodatetime (Startsecond). year;
  80. ???????????? Case "m":
  81. ???????????????? return Utcsecondtodatetime (Endsecond). Year * + utcsecondtodatetime (endsecond). Month-utcsecondtodatetime (Startsecond). Year * 12-utcsecondtodatetime (Startsecond). Month;
  82. ???????????? Case "D":
  83. ???????????????? return New TimeSpan (0, 0, 0, Endsecond-startsecond). Totaldays;
  84. ???????????? Case "h":
  85. ???????????????? return New TimeSpan (0, 0, 0, Endsecond-startsecond). TotalHours;
  86. ???????????? Case "mi":
  87. ???????????????? return New TimeSpan (0, 0, 0, Endsecond-startsecond). Totalminutes;
  88. ??
  89. ???????????? Case "s":
  90. ???????????? default:
  91. ???????????????? return Endsecond-startsecond;
  92. ????????}
  93. ????}
  94. ???? Public string Distance ()
  95. ???? {
  96. ???????? return New TimeSpan (0, 0, 0, Endsecond-startsecond). ToString ();
  97. ??
  98. ????}
  99. ???? #region get UTC
  100. ??
  101. ???? Public Static int Datetimetoutcsecond (datetime datetime)
  102. ???? {
  103. ???????? return (int) datetime.touniversaltime (). Subtract (UTC). TotalSeconds;
  104. ????}
  105. ??
  106. ???? Static DateTime utcsecondtodatetime (int second)
  107. ???? {
  108. ???????? return Utc. AddSeconds (second). ToLocalTime ();
  109. ????}
  110. ???? #endregion
  111. ??
  112. ???? //This is a placeholder member field
  113. ???? Public int Startsecond;
  114. ???? Public int Endsecond;
  115. ???? Public Static ReadOnly DateTime UTC = TIMEZONEINFO.CONVERTTIMETOUTC (new DateTime (1970, 1, 1), TIMEZONEINFO.UTC);
  116. ???? //Private member
  117. ???? Private bool _null;
  118. }

??

??

??

Using a user-defined type CLR UDT

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.