日期:2014-05-17  浏览次数:20612 次

全角半角混合字段截取问题
SQL 2000中文版

select 
cast(substring(cast('一副接W头/PCS' as varbinary) + cast(space(40) as varbinary),1,40) as varchar(40)) f1,
datalength(
cast(substring(cast('一副接W头/PCS' as varbinary) + cast(space(40) as varbinary),1,40) as varchar(40))
) f2, 
cast(substring(cast('一副接W头/PCS' as varbinary) + cast(space(40) as varbinary),41,40) as varchar(40)) f3,
datalength(
cast(substring(cast('一副接W头/PCS' as varbinary) + cast(space(40) as varbinary),41,40) as varchar(40))
) f4,
cast(substring(cast('一副接W头/PCS' as varbinary) + cast(space(40) as varbinary),81,40) as varchar(40)) f5,
datalength(
cast(substring(cast('一副接W头/PCS' as varbinary) + cast(space(40) as varbinary),81,40) as varchar(40))
) f6


f1 f2 f3 f4 f5 f6
-------------------------------------------------
一副接W头/PCS 40 3 0

(1 行受影响)


我想要的结果:
这个字段内的字符串 '一副接W头/PCS' 后不足长度补40个空格
f1 f2 f3 f4 f5 f6
-------------------------------------------------
一副接W头/PCS 40 40 40



有什么好方法解决吗? 


------解决方案--------------------
SQL code
select  
cast(substring(cast('一副接W头/PCS' as varbinary(400)) + cast(space(400) as varbinary(400)),1,40) as varchar(40)) f1,
datalength(
cast(substring(cast('一副接W头/PCS' as varbinary(400)) + cast(space(400) as varbinary(400)),1,40) as varchar(40))
) f2,  
cast(substring(cast('一副接W头/PCS' as varbinary(400)) + cast(space(400) as varbinary(400)),41,40) as varchar(40)) f3,
datalength(
cast(substring(cast('一副接W头/PCS' as varbinary(400)) + cast(space(400) as varbinary(400)),41,40) as varchar(40))
) f4,
cast(substring(cast('一副接W头/PCS' as varbinary(400)) + cast(space(400) as varbinary(400)),81,40) as varchar(40)) f5,
datalength(
cast(substring(cast('一副接W头/PCS' as varbinary(400)) + cast(space(400) as varbinary(400)),81,40) as varchar(40))
) f6
-----------------
f1                                       f2          f3                                       f4          f5                                       f6
---------------------------------------- ----------- ---------------------------------------- ----------- ---------------------------------------- -----------
一副接W头/PCS                                40                                                   40                                                   40

(1 行受影响)