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

      <bdo id='FYBSl'></bdo><ul id='FYBSl'></ul>

      1. <legend id='FYBSl'><style id='FYBSl'><dir id='FYBSl'><q id='FYBSl'></q></dir></style></legend>
      2. <small id='FYBSl'></small><noframes id='FYBSl'>

        <tfoot id='FYBSl'></tfoot>

        T-SQL:四舍五入到最接近的 15 分钟间隔

        时间:2023-07-17

        <legend id='mL3om'><style id='mL3om'><dir id='mL3om'><q id='mL3om'></q></dir></style></legend>

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

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

            • <bdo id='mL3om'></bdo><ul id='mL3om'></ul>
                <tbody id='mL3om'></tbody>

                • <tfoot id='mL3om'></tfoot>
                  本文介绍了T-SQL:四舍五入到最接近的 15 分钟间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  将 HH:MM 值四舍五入到最接近的 15 分钟间隔的最佳方法是什么?我不跟踪秒,所以它们无关紧要.

                  What's the best way to round an HH:MM value to the closest 15 minute interval? I don't track seconds so they don't matter.

                  00:08:00 becomes 00:15:00 
                  00:07:00 becomes 00:00:00 
                  01:59:00 becomes 02:00:00 
                  

                  等等.是否有一种优雅的、非 UDF 或 Case 语句方法来执行此操作?

                  and so on. Is there an elegant, non UDF or Case statement method for doing this?

                  这是我用来获取我想要四舍五入的上述值的 SQL:

                  Here's the SQL I'm using to get the above values that I'd like to round:

                  CONVERT(CHAR(8), DATEADD(n, SUM(DATEDIFF(n, starttime, stoptime)), 0), 108)
                  

                  starttimestoptime 是 SQL datetimes.

                  starttime and stoptime are SQL datetimes.

                  推荐答案

                  这里已回答 如何在 T-SQL 中舍入时间,我认为它应该适合您.

                  This was answered here How to Round a Time in T-SQL and i think it should work for you to.

                  CREATE FUNCTION [dbo].[RoundTime] (@Time datetime, @RoundTo float) RETURNS datetime
                  AS
                  BEGIN
                      DECLARE @RoundedTime smalldatetime, @Multiplier float
                  
                      SET @Multiplier = 24.0 / @RoundTo
                  
                      SET @RoundedTime= ROUND(CAST(CAST(CONVERT(varchar, @Time, 121) AS datetime) AS float) * @Multiplier, 0) / @Multiplier
                  
                      RETURN @RoundedTime
                  END
                  
                  -- Usage    
                  SELECT dbo.RoundTime('13:15', 0.5)
                  

                  这篇关于T-SQL:四舍五入到最接近的 15 分钟间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:具有多列聚合的 SQL Server 数据透视表 下一篇:如何对一个实例上的所有数据库运行相同的查询?

                  相关文章

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

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

                    <legend id='tK9bI'><style id='tK9bI'><dir id='tK9bI'><q id='tK9bI'></q></dir></style></legend>
                  1. <tfoot id='tK9bI'></tfoot>

                    • <bdo id='tK9bI'></bdo><ul id='tK9bI'></ul>