create table tb(col varchar(20)) insert into tb values('0101020000116300') insert into tb values('0101020000100200') insert into tb values('0101060000100600') insert into tb values('0101080000131900') go update tb set col = left(replace(col,'0000',''),len(replace(col,'0000',''))-2) select * from tb drop table tb /* col -------------------- 0101021163 0101021002 0101061006 0101081319 (所影响的行数为 4 行) */
create table tb(col varchar(20)) insert into tb values('0101020000116300') insert into tb values('0101020000100200') insert into tb values('0101060000100600') insert into tb values('0101080000131900') go update tb set col = left(col,6) + substring(col,11,4) select * from tb drop table tb /* col -------------------- 0101021163 0101021002 0101061006 0101081319 (所影响的行数为 4 行) */
先谢谢各位了,我还得对问题补充一下,是有上万条类似的记录,我不可能一条一条的使用 insert into ……value这条语句,应该做个循环,但是表中没有对这些记录顺序编号,该如何操作?
insert into tb values('0101020000116300')
insert into tb values('0101020000100200')
insert into tb values('0101060000100600')
insert into tb values('0101080000131900')
go
update tb
set col = left(replace(col,'0000',''),len(replace(col,'0000',''))-2)
select * from tb
drop table tb
/*
col
--------------------
0101021163
0101021002
0101061006
0101081319
(所影响的行数为 4 行)
*/
insert into tb values('0101020000116300')
insert into tb values('0101020000100200')
insert into tb values('0101060000100600')
insert into tb values('0101080000131900')
go
update tb
set col = left(col,6) + substring(col,11,4)
select * from tb
drop table tb
/*
col
--------------------
0101021163
0101021002
0101061006
0101081319
(所影响的行数为 4 行)
*/
insert into ……value这条语句,应该做个循环,但是表中没有对这些记录顺序编号,该如何操作?
select stuff(stuff ('0101020000116300',7,4,''),11,2,'')
--非固定位置的
select replace(replace('0101020000116300','0000',''),'00','')
--非固定位置的 只针对成倍的
select replace('0101020000116300','00','')
select stuff(stuff (字段名,7,4,''),11,2,'')
先谢谢各位了,我还得对问题补充一下,是有上万条类似的记录,我不可能一条一条的使用
insert into ……value这条语句,应该做个循环,但是表中没有对这些记录顺序编号,该如何操作?
我的insert into 只是测试数据,你不必输入的.使用我的update语句即可.
update 表名 set 字段名 = stuff(stuff (字段名,7,4,''),11,2,'')
update 表名
set 列名=left(列名,6)+substring(列名,11,4)