title: Not all LSNs map to dates
body: <p>I'm building an ETL that processes data from SQL server's change data capture feature. Part of the ETL is recording logs about the data that is processed including the data import window start and end. To do this I use the function <code>sys.fn_map_lsn_to_time()</code> to map the LSNs used to import the data to the corresponding datetime values.</p>
  
  <p>The function <code>sys.fn_cdc_get_all_changes_()</code> takes two parameters that are the start and end of the data import window. These parameters are inclusive so the next run needs to increment the previous LSN to avoid re-importing rows that fall on the boundary.</p>
  
  <p>The obvious answers is to use the function <code>sys.fn_cdc_increment_lsn()</code> to get the next LSN before bringing in the data. However, what I found is that this LSN does not always map to a datetime using <code>sys.fn_map_lsn_to_time()</code>. The LSN is valid for use in the <code>sys.fn_cdc_get_all_change_()</code> but I would like to be able to easily and accurately log the dates that are being used.</p>
  
  <p>For example:</p>
  
  <pre class="lang-sql prettyprint-override"><code>DECLARE @state_lsn_str CHAR(22) = '0x0000EEE100003E16008F'; -- try using `sys.fn_cdc_get_min_lsn(&lt;capture_instance&gt;)` instead since this value won't work for anyone else
  DECLARE @state_lsn BINARY(10) = CONVERT(BINARY(10), @state_lsn_str, 1);
  DECLARE @incr_lsn BINARY(10) = sys.fn_cdc_increment_lsn(@state_lsn);
  
  SELECT CONVERT(CHAR(22), @incr_lsn, 1) AS incremented_lsn,
         sys.fn_cdc_map_lsn_to_time(@incr_lsn) AS incremeneted_lsn_date;
  </code></pre>
  
  <p>This code returns an LSN value of <strong>0x0000EEE100003E160090</strong> and <strong>NULL</strong> for incremented_lsn_date</p>
  
  <hr>
  
  <p>Is there a way to force an LSN to be mapped to a time?
  <strong>OR</strong>
  Is there a way to get the next LSN that does map to a time without risking losing any data?</p>