当前位置: 代码迷 >> Sql Server >> sql批量替换有关问题
  详细解决方案

sql批量替换有关问题

热度:20   发布时间:2016-04-24 20:04:24.0
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
  相关解决方案