读书人

sql批量替换有关问题

发布时间: 2013-10-30 12:56:21 作者: rapoo

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

读书人网 >SQL Server

热点推荐