Share via

Issue with Declaring Length for VARCHAR Data Type in SQL Server Log Record Decoding

Vignesh K 20 Reputation points
2025-01-21T09:34:10.34+00:00

Hello team,

I am working on decoding SQL Server transaction log records, and I’ve successfully decoded all the data types so far. However, I’m facing an issue when declaring the length for VARCHAR because it is not a fixed size. For example, if we have VARCHAR(10) and only 5 bytes are used, the remaining 5 bytes are being used by the next data type.

Here's the query I’m using:

CONVERT(VARCHAR(10), SUBSTRING([RowLog Contents 0], 16, 10))

What is the correct way to declare the length of the VARCHAR in such cases, ensuring that the remaining unused bytes don’t affect the next data type?

Any guidance or suggestions would be greatly appreciated!

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


1 answer

Sort by: Most helpful
  1. Erland Sommarskog 134.7K Reputation points MVP Volunteer Moderator
    2025-01-21T22:41:27.28+00:00

    I have not paid much attention to how data is stored in the transaction log, but in the disk format, a variable-string is preceded by 1, 2 or 4 bytes indicating the length. For varchar(10), that would be one byte. So you need to read that byte first to get the argument to substring.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.