[freetds] String comparisons not working correctly, FreeTDS + PHP + MSSQL 2005

Reid, Roger L. roger.reid at dpw.com
Wed Apr 30 13:44:17 EDT 2008


Although I have a couple questions I think there's may be an issue that your literal 'Fred' is a char(4) and your field is nvarchar(15).

This might be collation sensitive too.  I know I have had similar issues, but just now I could not repeat it.

The problem isn't char vs varchar, but the nvarchar.   It's a Unicode string in the database.   It needs to convert SOMETHING to do the comparison, since 'Fred' is not a Unicode string.   Easy check (and fix) - prepend N to 'Fred' thus:

	where UweName = N'Fred';

What your query is doing is correct for a fixed width char field - if UweName were char(15), you'd need to do
	where rtrim(UweName) = 'Fred';
but that could be a lot of work for the server in some circumstances.

There's probably an implicit conversion happening somewhere - and it's converting the field into a char(15) before comparing.

Try the N'Fred'.  If that doesn't help, my question is - have you seen this query work the way you expect it to work in another application (e.g. isql, sqsh, MS SQL Server Manager?

And check that the field is a varchar of some stripe and and a char.  Even if you don't figure it out, there should be no ugly hacks needed,
just casting some data.

======================

So, I deleted the WHERE clause, and used php's var_dump function on
each row that was retrieved from the database. And this is what i got:
"UweName" => "Fred           " (The actual value, and trailing spaces
to fill the 15 characters of the column).




More information about the FreeTDS mailing list