How to fix FreeTDS character encoding issues

1 October 2022

The business I currently work for host websites on Linux servers that communicate with Microsoft SQL databases, our product then imports data from the external database into our MySQL database.

For many years the product was plagued with character encoding issues whilst using FreeTDS, that would sometimes be resolved by changing the encoding using PHP, but most of the time data was past the point of saving. Until I stumbled upon a Stackoverflow article (that was slightly unrelated to the issue at hand) and saw a slightly different ODBC configuration and that was the fix!

Solution

Configure your freetds.conf and odbc.ini to specify the client charset

freetds.conf

[mssql]
host = ntbox.mydomain.com
port = 1433
client charset = UTF-8
tds version = 7.4

odbc.ini

[mssql]
Driver = FreeTDS
Servername = mssql
TDS_Version = 7.4
Port = 1433
Trace = No
ClientCharset = UTF-8