选择空白字段时,停止SQL Server 2008检索“空格”字符

时间:2013-10-10 15:16:51

标签: php sql-server sql-server-2008 null field

我遇到了最近出现的SQL Server问题。当select语句调用一个空字段(没有NULL值但内部没有数据)时,它会产生一个'space'字符。我一直在使用2x 2008 R2 SQL Server进行测试,其中一个是有效的,有一个没有。 Windows没有更新,PHP版本相同。这只是随机开始的。 任何想法?

1 个答案:

答案 0 :(得分:0)

您确实需要查看数据字段中存储的内容。这是第一个开始的地方!

它是否在表定义中定义为非空。

以下示例设置临时表,最后三个条目看起来像空白。

--
-- Setup sample table
--

-- Sample table
create table #blanks
( 
    blanks_id int identity (1, 1),
    blanks_txt varchar(64)
);
go

-- Add null, space, empty string, hex codes
insert into #blanks (blanks_txt) values (null);
insert into #blanks (blanks_txt) values (space(10));
insert into #blanks (blanks_txt) values ('');
insert into #blanks (blanks_txt) values (char(9));
go

-- Last three entries show as blank
select * from #blanks

使用以下代码段找出该字段中的内容。

-- Code to find out internal data values
select 
    blanks_id,
    datalength(blanks_txt) as size, 
    cast(blanks_txt as varbinary(64)) as buffer 
from #blanks

两次代码执行的输出。

enter image description here