SQL 修改字段

来源:百度知道 编辑:UC知道 时间:2024/07/08 21:50:16
如果A列以123开头,则
123后面追加一个‘-’


32444444
12345211
12345642

修改为
32444444
123-45211
123-45642
谢谢

*******************
补充:调试例如一下有点错误改正了。
*******************
oracle中:
update 表名 set a=substr(a,1,3)||'-'||substr(a,4,length(a)-4) where substr(a,1,3)='123';

SqlServer中:
update 表名 set a=substring(a,1,3)+'-'+substring(a,4,len(a)) where substring(a,1,3)='123';

说明:根据你的描述,你的字段既然能够容纳'-'所以一定是字符类型的,所以写了上面的sql语句。

---
以上,希望对你有所帮助。

use Tempdb
go
--> -->

if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([Col] nvarchar(20))
Insert #T
select 32444444 union all
select 12345211 union all
select 12345642
Go
update #T
set Col=stuff(Col,4,0,'-')
where Col like '123[^-]%'

select * from #T

Col
--------------------
32444444
123-45211
123-45642

(3 个资料列受到影响)