日期:2014-05-18 浏览次数:20507 次
SELECT DATALENGTH(c1) as c1len, DATALENGTH(c2) as c2len FROM t1
------解决方案--------------------
如楼上所说,char型数据,如果字符串没有填满,后面则跟随' '
代码更改为如下即可:
CREATE TABLE t1 ( c1 varchar(3), c2 char(3) ) GO INSERT INTO t1 VALUES ('2', '2') INSERT INTO t1 VALUES ('37', '37') INSERT INTO t1 VALUES ('597', '597') GO SELECT REPLICATE('0', 3 - DATALENGTH(c1)) + c1 AS 'Varchar Column', REPLICATE('0', 3 - DATALENGTH(rtrim(c2))) + c2 AS 'Char Column' FROM t1 drop table t1 /* Varchar Column Char Column -------------- ------------ 002 002 037 037 597 597 (所影响的行数为 3 行) */