首页 > 解决方案 > 从模式中删除字符串中的特定字符

问题描述

向 sql 写入查询时出现问题。问题如下:有一个 FILE_NAME 列的填充表,在此列中需要删除某些元素中的一行中的数字 0。这个 0 总是在行中的同一个位置。下面我附上数据截图,黄色字符一定要去掉。

问题

标签: sqlsql-server

解决方案


通过使用SUBSTRINGLEN函数作为下一个模式尝试这种方法:-

Update tableName
Set columnName = substring(columnName , 0, 9) +
                 substring(columnName , 10, len(columnName ))
Where substring(columnName , 9, 1) = '0' 
And len(columnName ) > 15

演示

Create table #Temp (Col1 varchar(20))
insert into #Temp values ('18_0231_0121_001') -- Remove the 0 in index #9
insert into #Temp values ('18_0231_0121_12') -- keep the 0 in index #9
insert into #Temp values ('18_0231_2121_001') -- there is no 0 in index #9, so keep it as it is

select * from #Temp

更新前的结果

18_0231_0121_001
18_0231_0121_12
18_0231_2121_001

使用更新作为下一步

update #Temp 
set Col1 = substring(Col1, 0, 9) + substring(Col1, 10, len(Col1))
where substring(Col1, 9, 1) = '0' and len(Col1) > 15

select * from #Temp

更新后的结果

18_0231_121_001
18_0231_0121_12
18_0231_2121_001

推荐阅读