sql - 从模式中删除字符串中的特定字符
问题描述
向 sql 写入查询时出现问题。问题如下:有一个 FILE_NAME 列的填充表,在此列中需要删除某些元素中的一行中的数字 0。这个 0 总是在行中的同一个位置。下面我附上数据截图,黄色字符一定要去掉。
解决方案
通过使用SUBSTRING和LEN函数作为下一个模式尝试这种方法:-
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
推荐阅读
- laravel - 如何在 laravel 中测试 back() 方法
- javascript - 在 Google Apps 脚本中从服务器端 (.gs) 传递到客户端 (html) 时,已知类型的变量变为“未定义”
- python - 如何提高 Pytesseract 提取数字的准确性
- excel - VBA中的名称和全名有什么区别?
- git - 如何压缩给定提交后所做的更改?(混帐存档)
- aspnetboilerplate - 是否有任何代码生成器(内置或第三方)可用于为 ASP..NET 样板生成数据库优先代码?
- python - 在数据边界处寻找峰值
- reactjs - undefined 不是 React Native 中的函数
- javascript - 将数组合并为一个
- php - jQuery/PHP 文件上传在 99% 处重新启动,然后返回超时