[freetds] select returns a " " character instead of ""

michael.peppler@bnpparibas.com michael.peppler at bnpparibas.com
Thu Apr 6 02:00:03 EDT 2006


This isn't a freetds issue, but rather an MS-SQL (and Sybase) issue.

If you store an empty string into a non-null varchar column the db server
will in fact store a single space character. This is to distinguish the
"empty" string with the NULL value (which has 0 length).

Michael




Internet
martin.rode at programmfabrik.de@lists.ibiblio.org - 05/04/2006 20:00


Please respond to freetds at lists.ibiblio.org

Sent by:    freetds-bounces at lists.ibiblio.org

To:    freetds

cc:


Subject:    [freetds] select returns a " " character instead of ""


hi everybody,

i have found another problem, this time with a current version of php:

client:
* debian linux
* php 5.1.2
* tds 0.63 (debian unstable)

server:
* using microsoft sql server express 9.0.1399 on windows xp2

this following script. it should return a length of 0 for the string
data, but it returns 1.

i see the same problem with the php/sybase_* api.

--------------------------------
test_query2();


function test_query2() {
    $dsn ="DRIVER={FreeTDS};SERVER=thepope;DATABASE=test";
    $link2 = odbc_connect($dsn , 'sa', 'test' ) or die(odbc_errormsg() );
    odbc_autocommit($link2, true);
    odbc_exec($link2, "
IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_NAME = 'images') DROP TABLE [images];\n;
CREATE TABLE images (
   name varchar(50),
   data varchar(max)
);
");
    $ins = "INSERT INTO images (name,data) VALUES ('henk','')";
    odbc_exec($link2, $ins);

    $sql = "SELECT data,datalength(data) as length FROM images";
    $res = odbc_exec($link2, $sql);
    $row = odbc_fetch_array($res);
    echo "<pre>";
    print_r($row);
    print_r("STRLEN of 'data':".strlen($row[data]));
    // this prints a length of 1, but it should be 0
}

_______________________________________________
FreeTDS mailing list
FreeTDS at lists.ibiblio.org
 http://lists.ibiblio.org/mailman/listinfo/freetds



This message and any attachments (the "message") is
intended solely for the addressees and is confidential. 
If you receive this message in error, please delete it and 
immediately notify the sender. Any use not in accord with 
its purpose, any dissemination or disclosure, either whole 
or partial, is prohibited except formal approval. The internet
can not guarantee the integrity of this message. 
BNP PARIBAS (and its subsidiaries) shall (will) not 
therefore be liable for the message if modified. 

                ---------------------------------------------

Ce message et toutes les pieces jointes (ci-apres le 
"message") sont etablis a l'intention exclusive de ses 
destinataires et sont confidentiels. Si vous recevez ce 
message par erreur, merci de le detruire et d'en avertir 
immediatement l'expediteur. Toute utilisation de ce 
message non conforme a sa destination, toute diffusion 
ou toute publication, totale ou partielle, est interdite, sauf 
autorisation expresse. L'internet ne permettant pas 
d'assurer l'integrite de ce message, BNP PARIBAS (et ses
filiales) decline(nt) toute responsabilite au titre de ce 
message, dans l'hypothese ou il aurait ete modifie.



More information about the FreeTDS mailing list