SQL表中怎么去掉一个字段中的空字符

不是空格,也不是 字符,用这两个查不出来,那个字段是存的序列号(code)

比方说一行的值是:2012
直接用
select * from table where code='2012'--这样是查不到的
select * from table where code like'%2012'--这样可以查到

select len(code) from table where code like'%2012' --返回的是5

查询空格和空字符 查询不到任何结构
select * from table where code like '% %' --一个也查不到

我想知道前面那个看不见但是存在的字符是什么...

用这种替换语句
update table set code=ltrim(rtrim(code))--执行后没有效果,可见不是空格

update table set code=replace(code,' ','')--执行后也是没有效果

用REPLACE函数来替换。
SQL字段中的空字符有2种,空格符和tab制表符(即char(9))。
例:去除 表table 中 col 列的空字符
去除空格符:update table set col = REPLACE(col,' ','')
去除制表符:update table set col = REPLACE(col,CHAR(9),'')
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-03-02
你可以先用 select ascii(code) from table 来判断前面的到底是什么字符,之后再将该字符替换为空字符串。

或者确定每条记录前面都有这么一个字符的话,直接用:
update table set code=right(code,len(code)-1) where len(code)>1 将前面的字符截掉本回答被提问者采纳
第2个回答  2012-03-02
--以下是我测试用的语句, 你看看参考吧
declare @a varchar(20)
set @a = 'abc
'
--这里查询是为了看一下这个字段左边的那个字段ASCII码是什么
select ascii(left(@a, 1))

--select case when right(@a, 1) = nchar(10) then 1 else 0 end

--我想你的Update可以这么写
update table1 set code = case when left(code, 1) = nchar(10) then right(code, len(code)-1) else code end
第3个回答  2012-03-02
那一列是什么数据类型呢?varchar?本回答被网友采纳
相似回答