Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. DECLARE @StartDate DATE = '20110901'
  2. , @EndDate DATE = '20111001'
  3.  
  4. SELECT DATEADD(DAY, nbr - 1, @StartDate)
  5. FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY c.object_id ) AS Nbr
  6. FROM sys.columns c
  7. ) nbrs
  8. WHERE nbr - 1 <= DATEDIFF(DAY, @StartDate, @EndDate)
  9.  
  10. WITH Dates AS (
  11. SELECT
  12. [Date] = CONVERT(DATETIME,'09/01/2011')
  13. UNION ALL SELECT
  14. [Date] = DATEADD(DAY, 1, [Date])
  15. FROM
  16. Dates
  17. WHERE
  18. Date < '10/10/2011'
  19. ) SELECT
  20. [Date]
  21. FROM
  22. Dates
  23. OPTION (MAXRECURSION 45)
  24.  
  25. DECLARE @dates TABLE(dt datetime)
  26. DECLARE @dateFrom datetime
  27. DECLARE @dateTo datetime
  28.  
  29. SET @dateFrom = '2001/01/01'
  30. SET @dateTo = '2001/01/12'
  31.  
  32. WHILE(@dateFrom < @dateTo)
  33. BEGIN
  34. SELECT @dateFrom = DATEADD(day, 1,@dateFrom)
  35. INSERT INTO @dates
  36. SELECT @dateFrom
  37. END
  38.  
  39. SELECT * FROM @dates
  40.  
  41. --Generate a range of dates with interval option, courtesy of Abe Miessler for the core query here!
  42. ALTER FUNCTION [dbo].[DateRange]
  43. (@startDate AS DATE,
  44. @EndDate AS DATE,
  45. @interval AS INT
  46. )
  47. RETURNS @Dates TABLE(dateValue DATE)
  48. AS
  49. BEGIN
  50. WITH Dates
  51. AS (
  52. SELECT [Date] = CONVERT( DATETIME, @startDate)
  53. UNION ALL
  54. SELECT [Date] = DATEADD(DAY, ISNULL(@interval, 1), [Date])
  55. FROM Dates
  56. WHERE Date < @EndDate)
  57. INSERT INTO @Dates
  58. SELECT [Date]
  59. FROM Dates
  60. OPTION(MAXRECURSION 900);
  61. RETURN;
  62. END;
  63.  
  64. Declare @StartDate datetime = '2015-01-01'
  65. Declare @EndDate datetime = '2016-12-01'
  66. declare @DaysInMonth int
  67. declare @tempDateRange Table
  68. (
  69. DateFrom datetime,
  70. DateThru datetime
  71. );
  72.  
  73. While @StartDate<=@EndDate
  74. begin
  75. SET @DaysInMonth=DAY(DATEADD(DD,-1,DATEADD(MM,DATEDIFF(MM,-1,@StartDate),0)))
  76.  
  77. IF DAY(@StartDate)=1
  78. SET @EndDate=DATEADD(DAY,14,@StartDate)
  79. ELSE IF DAY(@StartDate)=16 AND @DaysInMonth=30
  80. SET @EndDate=DATEADD(DAY,14,@StartDate)
  81. ELSE IF DAY(@StartDate)=16 AND @DaysInMonth=31
  82. SET @EndDate=DATEADD(DAY,15,@StartDate)
  83. ELSE IF DAY(@StartDate)=16 AND @DaysInMonth=28
  84. SET @EndDate=DATEADD(DAY,12,@StartDate)
  85. ELSE IF DAY(@StartDate)=16 AND @DaysInMonth=29
  86. SET @EndDate=DATEADD(DAY,13,@StartDate)
  87.  
  88. INSERT INTO @tempDateRange (DateFrom,DateThru)
  89. VALUES
  90. (
  91. @StartDate,
  92. @EndDate
  93. )
  94.  
  95. SET @StartDate=DATEADD(DAY,1,@EndDate)
  96.  
  97. IF @EndDate< '2016-12-31'
  98. IF DAY(@StartDate)=1
  99. SET @EndDate=DATEADD(DAY,14,@StartDate)
  100. ELSE IF DAY(@StartDate)=16 AND @DaysInMonth=30
  101. SET @EndDate=DATEADD(DAY,14,@StartDate)
  102. ELSE IF DAY(@StartDate)=16 AND @DaysInMonth=31
  103. SET @EndDate=DATEADD(DAY,15,@StartDate)
  104. ELSE IF DAY(@StartDate)=16 AND @DaysInMonth=28
  105. SET @EndDate=DATEADD(DAY,12,@StartDate)
  106. ELSE IF DAY(@StartDate)=16 AND @DaysInMonth=29
  107. SET @EndDate=DATEADD(DAY,13,@StartDate)
  108. end ;
  109.  
  110. select * from @tempDateRange
  111.  
  112. +++++++++++++++++++++++++++++
  113. Result:
  114. DateFrom |DateThru
  115.  
  116. select
  117. dateadd(day, nbr - 1, convert(date, '2017-01-01')) as d
  118. from (
  119. select row_number() over (order by c.object_id) as nbr from sys.columns c
  120. ) nbrs
  121. where
  122. nbr - 1 <= datediff(
  123. day,
  124. convert(date, '2017-01-01'),
  125. convert(date, '2018-12-31')
  126. )
  127.  
  128. view: date_series {
  129. derived_table: {
  130. sql:
  131. select
  132. dateadd(day, nbr - 1, convert(date, '2017-01-01')) as d
  133. from (
  134. select row_number() over (order by c.object_id) as nbr from sys.columns c
  135. ) nbrs
  136. where
  137. nbr - 1 <= datediff(day, convert(date, '2017-01-01'), convert(date, '2018-12-31')) ;;
  138. }
  139.  
  140. dimension: date {
  141. primary_key: yes
  142. type: date
  143. sql: ${TABLE}.d ;;
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement