Sybase 插入一个 space 来代替空字符串 ''

Sybase inserts a single space in place of the empty string ''

显然,当在 VARCHAR 列中插入空字符串 ('') 时,Sybase(在 ASE 15.7 中测试)改为插入单个 space 字符。通过实验,我验证了选项 ansinull 与此行为无关:

> set ansinull on
> create table a (a1 varchar(1))
> insert into a(a1) values ('')
> select a1, len(a1) as 'len(a1)', datalength(a1) as 'datalength(a1)',
  ascii(a1) as 'ascii(a1)', char_length(a1) as 'char_length(a1)'
  from a
> go
(1 row affected)
a1 len(a1)     datalength(a1) ascii(a1)   char_length(a1)
-- ----------- -------------- ----------- ---------------
             1              1          32               1

(1 row affected)
>
>
> drop table a
> go
> set ansinull off
> create table a (a1 varchar(1))
> insert into a(a1) values ('')
> select a1, len(a1) as 'len(a1)', datalength(a1) as 'datalength(a1)',
  ascii(a1) as 'ascii(a1)', char_length(a1) as 'char_length(a1)'
  from a
> go
(1 row affected)
a1 len(a1)     datalength(a1) ascii(a1)   char_length(a1)
-- ----------- -------------- ----------- ---------------
             1              1          32               1

(1 row affected)

这种行为是否有任何理由/推理?我该如何禁用它 "feature"?此行为是否在 SQL 服务器代码库中继承?

我被这个困扰了,因为我的测试逻辑失败了,因为我正在做一个 .equals() 比较(在使用 JDBC 从数据库读取的客户端 Java 代码中并做出某些断言)。

回复:"Is this behavior inherited in the SQL Server codebase?" - 否

回复:"Is there any justification / reasoning for this behavior and how can I disable this "功能“?” - 不是我知道的。这是众多 Sybase 怪癖之一。

来自http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc36271.1550/html/blocks/blocks311.htm

The empty string ("") or ('') is interpreted as a single blank in insert or assignment statements on varchar or univarchar data. In concatenation of varchar, char, nchar, nvarchar data, the empty string is interpreted as a single space; for following example is stored as “abc def”:

"abc" + "" + "def"

The empty string is never evaluated as NULL.

据记忆:在 Sybase 中,您可以将 NULL 值分配给字符串,它将被解释为空字符串。 - 不过我可能是错的。

另一种解决方法是 rtrim( '' )

ASE 在 DBMS 世界中是独一无二的,因为空字符串实际上计算为单个 space。正如有人提到的那样,当您连接 NULL 时,它充当真正的空字符串。