Beware this content is over a year old and considered stale. It may no longer be accurate and/or reflect the understanding of the author but remains here for reference only. Please keep this in mind as you use this content.

If you’re using PHP 5 and MSSQL extension to retrieve data from Microsoft SQL Server and you’re seeing your columns truncated at around the 30 character mark, it could be down to your FreeTDS settings.

For context, I’m using Ubuntu 14.04 (Trusty) and when I run the following command, I can see the following output:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ tsql -C output
Compile-time settings (established with the "configure" script)
Version: freetds v0.91
freetds.conf directory: /etc/freetds
MS db-lib source compatibility: no
Sybase binary compatibility: yes
Thread safety: yes
iconv library: yes
TDS version: 4.2
iODBC: no
unixodbc: yes
SSPI "trusted" logins: no
Kerberos: yes

The quick and easy way to do this is simply to install the FreeTDS kitchen-sink which gives you access to further settings and generally better debugging support.

1
2
3
apt-get remove --purge freetds-common
apt-get install unixodbc
apt-get install freetds-bin freetds-common freetds-dev libct4 libsybdb5 php5-sybase

Then restart PHP and your web server, which in my case is nginx.

1
service php5-fpm restart && service nginx restart

You may also have to explicitly define the TDS protocol version to use. Add TDS protocol version to: /etc/freetds/freetds.conf

1
2
3
4
[global]
tds version = 7.1
# TDS protocol version
; tds version = 4.2

Further helpful information here:

  • PHP & SQL Server - field names truncated (StackOverflow)
  • SQL Server returned column name truncated to 29 characters using PHP (StackOverflow)