title: SQL Server Round a DateTime to the nearest 5 seconds?
body: <p>Just wondering if anyone can help me with this. I would like to round the SQL datetime to the nearest 5 seconds</p>
  
  <p>For example the dates on the left should give me the following result on the right.</p>
  
  <ul>
  <li>1900-01-01 14:08:20.000 -> 1900-01-01 14:08:20.000 </li>
  <li>1900-01-01 14:08:21.000 -> 1900-01-01 14:08:20.000 </li>
  <li>1900-01-01 14:08:22.000 -> 1900-01-01 14:08:20.000 </li>
  <li>1900-01-01 14:08:23.000 -> 1900-01-01 14:08:25.000 </li>
  <li>1900-01-01 14:08:24.000 -> 1900-01-01 14:08:25.000 </li>
  <li>1900-01-01 14:08:25.000 -> 1900-01-01 14:08:25.000 </li>
  </ul>
  
  <p>I have seen a similar solution for minute intervals and I have tried to apply it to seconds but it doesn't give the ideal result. </p>
  
  <pre><code>declare @DT datetime
  
  set @DT = convert(datetime,'14:08:20')
  select dateadd(s,(datepart(s,dateadd(s,1,@DT))/5)*5,
      dateadd(mi,datediff(mi,0,dateadd(s,1,@DT)),0))
  </code></pre>
  
  <p>However, 1900-01-01 14:08:23.000 gives me  1900-01-01 14:08:20.000,  which is not correct. </p>
  
  <p>Any help is appreciated.</p>