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

Federico Cáceres fede.caceres at gmail.com
Fri May 2 09:34:27 EDT 2008

Thank you Roger.

SELECT * FROM table WHERE UweUser = N'Fred';
That worked like a charm!

Oddly enough, when using tsql, I could run the query without casting
(= N'...'), and it worked!

I am not sure what's going on with PHP and FreeTDS, I'll try some
additional tests, but at least the client is going to be happy his
application is up and running now :D.

Again, thank you very much!

On Wed, Apr 30, 2008 at 2:44 PM, Reid, Roger L. <roger.reid at dpw.com> wrote:
> 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).
>  _______________________________________________
>  FreeTDS mailing list
>  FreeTDS at lists.ibiblio.org
>  http://lists.ibiblio.org/mailman/listinfo/freetds

More information about the FreeTDS mailing list