title: SQL - Get custom day start datetime for passed datetime
body: <p>I am working on requirement where the day starts at 23:00 last day. 
  I need to get day start date time for passed date to SQL function. We are storing custom day start time (i.e. 23:00) and <code>offset = -1</code> (means starting last day) in table now.<br>
  e.g. If I pass :  </p>
  
  <ol>
  <li><code>10/30/2013 22:00</code> it should return <code>10/29/2013 23:00</code></li>
  <li><code>10/30/2013 23:20</code> it should return <code>10/30/2013 23:00</code></li>
  <li><code>10/31/2013 01:00</code> it should return <code>10/30/2013 23:00</code> </li>
  </ol>
  
  <p>Currently I am using following statement : <code>(DayStartOffset = -1 and DayStartTime = 23:00:00)</code></p>
  
  <pre><code>    declare @datetime datetime 
      set @datetime = '2013-10-30 23:59:59'
      declare @date date
      declare @time time(3)
      set @date = @datetime
      set @time = @datetime
      declare @dayStartDateTime datetime 
  
      --DayStartOffset is set to -1
      --DayStartTime set to '23:00'
      SELECT @dayStartDateTime = DATEADD(dd,DayStartOffset,CAST(@date AS DATETIME)) +                 CAST(DayStartTime AS DATETIME)
       from table_name
       print @dayStartDateTime
  </code></pre>
  
  <p>But it is not working correctly for e.g. 1 and 3 above</p>
  
  <p>Can you please help me on this function.</p>