<tfoot id='8IQJB'></tfoot>

    • <bdo id='8IQJB'></bdo><ul id='8IQJB'></ul>

    <small id='8IQJB'></small><noframes id='8IQJB'>

    <legend id='8IQJB'><style id='8IQJB'><dir id='8IQJB'><q id='8IQJB'></q></dir></style></legend>
    <i id='8IQJB'><tr id='8IQJB'><dt id='8IQJB'><q id='8IQJB'><span id='8IQJB'><b id='8IQJB'><form id='8IQJB'><ins id='8IQJB'></ins><ul id='8IQJB'></ul><sub id='8IQJB'></sub></form><legend id='8IQJB'></legend><bdo id='8IQJB'><pre id='8IQJB'><center id='8IQJB'></center></pre></bdo></b><th id='8IQJB'></th></span></q></dt></tr></i><div id='8IQJB'><tfoot id='8IQJB'></tfoot><dl id='8IQJB'><fieldset id='8IQJB'></fieldset></dl></div>

        在 TSQL 中生成递增日期的结果集

        Generate a resultset of incrementing dates in TSQL(在 TSQL 中生成递增日期的结果集)

        <legend id='1mPA4'><style id='1mPA4'><dir id='1mPA4'><q id='1mPA4'></q></dir></style></legend>

      1. <tfoot id='1mPA4'></tfoot>

            <tbody id='1mPA4'></tbody>
          <i id='1mPA4'><tr id='1mPA4'><dt id='1mPA4'><q id='1mPA4'><span id='1mPA4'><b id='1mPA4'><form id='1mPA4'><ins id='1mPA4'></ins><ul id='1mPA4'></ul><sub id='1mPA4'></sub></form><legend id='1mPA4'></legend><bdo id='1mPA4'><pre id='1mPA4'><center id='1mPA4'></center></pre></bdo></b><th id='1mPA4'></th></span></q></dt></tr></i><div id='1mPA4'><tfoot id='1mPA4'></tfoot><dl id='1mPA4'><fieldset id='1mPA4'></fieldset></dl></div>

            <bdo id='1mPA4'></bdo><ul id='1mPA4'></ul>

          • <small id='1mPA4'></small><noframes id='1mPA4'>

                • 本文介绍了在 TSQL 中生成递增日期的结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑需要创建日期结果集.我们有开始日期和结束日期,我们想生成一个介于两者之间的日期列表.

                  Consider the need to create a resultset of dates. We've got start and end dates, and we'd like to generate a list of dates in between.

                  DECLARE  @Start datetime
                           ,@End  datetime
                  DECLARE @AllDates table
                          (@Date datetime)
                  
                  SELECT @Start = 'Mar 1 2009', @End = 'Aug 1 2009'
                  
                  --need to fill @AllDates. Trying to avoid looping. 
                  -- Surely if a better solution exists.
                  

                  考虑带有 WHILE 循环的当前实现:

                  Consider the current implementation with a WHILE loop:

                  DECLARE @dCounter datetime
                  SELECT @dCounter = @Start
                  WHILE @dCounter <= @End
                  BEGIN
                   INSERT INTO @AllDates VALUES (@dCounter)
                   SELECT @dCounter=@dCounter+1 
                  END
                  

                  问题:您将如何使用 T-SQL 创建一组在用户定义范围内的日期?假设 SQL 2005+.如果您的答案是使用 SQL 2008 功能,请标记为此类.

                  Question: How would you create a set of dates that are within a user-defined range using T-SQL? Assume SQL 2005+. If your answer is using SQL 2008 features, please mark as such.

                  推荐答案

                  如果您的日期相隔不超过 2047 天:

                  If your dates are no more than 2047 days apart:

                  declare @dt datetime, @dtEnd datetime
                  set @dt = getdate()
                  set @dtEnd = dateadd(day, 100, @dt)
                  
                  select dateadd(day, number, @dt)
                  from 
                      (select number from master.dbo.spt_values
                       where [type] = 'P'
                      ) n
                  where dateadd(day, number, @dt) < @dtEnd
                  

                  在多次请求后,我更新了我的答案.为什么?

                  原始答案包含子查询

                   select distinct number from master.dbo.spt_values
                       where name is null
                  

                  结果与我在 SQL Server 2008、2012 和 2016 上的测试结果相同.

                  which delivers the same result, as I tested them on SQL Server 2008, 2012, and 2016.

                  然而,当我试图在从 spt_values 查询时分析 MSSQL 内部的代码时,我发现 SELECT 语句总是包含子句 WHERE [type]='[魔法代码]'.

                  However, as I tried to analyze the code that MSSQL internally when querying from spt_values, I found that the SELECT statements always contain the clause WHERE [type]='[magic code]'.

                  因此我决定虽然查询返回正确的结果,但由于错误的原因它提供了正确的结果:

                  Therefore I decided that although the query returns the correct result, it delivers the correct result for wrong reasons:

                  未来版本的 SQL Server 可能会定义不同的 [type] 值,该值也将 NULL 作为 [name] 的值>,在 0-2047 范围之外,甚至不连续,在这种情况下,结果将是完全错误的.

                  There may be a future version of SQL Server which defines a different [type] value which also has NULL as values for [name], outside the range of 0-2047, or even non-contiguous, in which case the result would be simply wrong.

                  这篇关于在 TSQL 中生成递增日期的结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                  SQL query to group by day(按天分组的 SQL 查询)
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)

                      <i id='sQxbw'><tr id='sQxbw'><dt id='sQxbw'><q id='sQxbw'><span id='sQxbw'><b id='sQxbw'><form id='sQxbw'><ins id='sQxbw'></ins><ul id='sQxbw'></ul><sub id='sQxbw'></sub></form><legend id='sQxbw'></legend><bdo id='sQxbw'><pre id='sQxbw'><center id='sQxbw'></center></pre></bdo></b><th id='sQxbw'></th></span></q></dt></tr></i><div id='sQxbw'><tfoot id='sQxbw'></tfoot><dl id='sQxbw'><fieldset id='sQxbw'></fieldset></dl></div>
                      <legend id='sQxbw'><style id='sQxbw'><dir id='sQxbw'><q id='sQxbw'></q></dir></style></legend>

                      <small id='sQxbw'></small><noframes id='sQxbw'>

                      • <tfoot id='sQxbw'></tfoot>

                          <tbody id='sQxbw'></tbody>
                          <bdo id='sQxbw'></bdo><ul id='sQxbw'></ul>