[英]How to remove zeros from a column in db2 table
I have a column with data zeros after 6th column 我在第六列之后有一个数据零的列
i want to remove the leading zero after the 6th pipe in the data. 我想删除数据中第6个管道之后的前导零。
Please let me know if there is any way to do it. 请让我知道是否有任何办法。 I tried to use
substr
with Trim
but its not working. 我试图将
substr
与Trim
结合使用,但无法正常工作。
假设您的列称为COL
,类似以下内容的代码应该起作用:
CONCAT(SUBSTR(1,INSTR(COL,'|', 1,5)), LTRIM(SUBSTR(INSTR(COL,'|', 1,5)+1)),'0'))
Assuming from your current data that you need to only replace every occurrence of |00
with |
假设从您当前的数据中,您只需要用
|
替换每次出现的|00
|
, You can achieve thing using REPLACE
function. ,您可以使用
REPLACE
功能来实现。
SELECT 'TMB|MLE020828585|74384911WA3S|="''07300058"|74384911|0013' AS Current_String,
replace('TMB|MLE020828585|74384911WA3S|="''07300058"|74384911|0013', '|00', '|') AS result_String
You can replace hard-coded value in above with your column name. 您可以将上面的硬编码值替换为列名。
The above query generate result as below. 上面的查询生成结果如下。
Current_String | result_String
------------------------------------------------------------------------------------------------------------------------
TMB|MLE020828585|74384911WA3S|="'07300058"|74384911|0013 | TMB|MLE020828585|74384911WA3S|="'07300058"|74384911|13
Hope this will help. 希望这会有所帮助。
Oleg was correct in utilizing INSTR(), that is how I would do it. Oleg在使用INSTR()时是正确的,这就是我要这样做的方式。 He was missing some arguments, though.
不过,他缺少一些论点。 Also, I wasn't able to get ltrim to work, so I cast it to an int instead.
另外,我无法使ltrim正常工作,因此我将其强制转换为int。 I tested and updated my table with your data using:
我使用以下数据对您的表进行了测试和更新:
UPDATE mytable SET mycolumn = Substr(mycolumn , 1, Instr(mycolumn , '|', 1, 5)) || 更新mytable SET mycolumn = Substr(mycolumn,1,Instr(mycolumn,'|',1,5))|| CAST(Substr(mycolumn , Instr(mycolumn , '|', 1, 5) + 1) AS INT)
CAST(Substr(mycolumn,Instr(mycolumn,'|',1,5)+1)AS INT)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.