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

sql批量替换问题
我有个字段全是“2010-10-”这种“-”为结尾的,我现在想批量把最后一个“-”去除掉,该用sql语句如何写。

------解决方案--------------------
适用于Mysql,left 函数有的,char_length计算字符的长度


update 你的表 
set 要替换的字段 = left(要替换的字段,CHAR_LENGTH(要替换的字段)-1)


------解决方案--------------------

create table tb(col varchar(10))
insert into tb
 select '2010-10-' union all
 select '2010-5-' union all
 select '2010-9-' union all
 select '2010-5-8'

update tb set col=left(col,len(col)-1)
where right(col,1)='-'

select * from tb
--结果
col
----------
2010-10
2010-5
2010-9
2010-5-8

(4 行受影响)

drop table tb