Sometimes you may come across an .mdf
file without knowing which version of SQL Server created it. Trying to attach it to a newer or older SQL Server instance might throw version incompatibility errors. Fortunately, you can determine the SQL Server version of an MDF file by inspecting a few specific bytes from it using PowerShell.
โ๏ธ Use PowerShell to Identify SQL Server Version
Open PowerShell and run the following command, replacing the path with your MDF file's location:
get-content -Encoding Byte "C:\NORTHWND.mdf" | select-object -skip 0x12064 -first 2
This command reads the MDF file as a byte array and retrieves two important bytes starting from the offset 0x12064
(73,572 in decimal), which hold the version information.
โ Example Output
86
3
Now calculate the version number using the formula:
Version = (SecondByte * 256) + FirstByte
For the example above:
Version = (3 * 256) + 86 = 854
๐๏ธ Version Mapping Reference
Once you calculate the version number, compare it with this quick reference to identify the corresponding SQL Server version:
Version Code | SQL Server Version |
---|---|
539 | SQL Server 7.0 |
611 | SQL Server 2005 |
655 | SQL Server 2008 |
661 | SQL Server 2008 R2 |
706 | SQL Server 2012 |
782 | SQL Server 2014 |
852 | SQL Server 2016 |
869 | SQL Server 2017 |
884 | SQL Server 2019 |
950 | SQL Server 2022 |
In our case, 854
corresponds to SQL Server 2016.
๐งช Alternative: Hex Editors
If you are not comfortable with PowerShell you can also open the MDF file using a hex editor like HxD or 010 Editor. Navigate to offset 0x12064
and read the two bytes. You will need to apply the same formula to calculate the version code.
๐ Final Tip
Keep in mind this method only works on primary MDF files that are not corrupted. Always ensure you have backups before attempting any manual inspections or attachments.