Convert an LSN from Hex to Decimal

Log Sequence Numbers (LSNs) can be displayed and consumed in several formats. This post shows how to quickly and easily convert between the “typical” hexidecimal format, and the equivalent decimal format. Hexidecimal format LSNs appear in result sets returned by fn_dblog, and look like 00000268:0000356c:0001. However, the sys.fn_dblog demands input LSNs be in decimal format. The decimal format looks like 616:13676:1 for that same LSN.

Jean-Frederic Schall's paiting of this lady is far nicer than my LSN conversion tool.

Jean-Frederic Schall’s paiting of this lady is far nicer than my LSN conversion tool.

Conversion consists of getting each chunk from the hexidecimal form, in varchar format, followed by converting that number to an integer, then re-assembling the three parts.

The output looks like:

╔═════════════╗
║ 616:13676:1 ║
╚═════════════╝

The code above can be easily converted to a schema-bound table-valued-function like this:

Let me know if you have any questions about this post.

Check out the rest of our series on internals.