删除前导零 [英] Remove leading zeros
问题描述
给出列中的数据,如下所示:
Given data in a column which look like this:
00001 00
00026 00
我需要使用SQL从值中删除空格和所有前导零之后的任何内容,以便最终输出为:
I need to use SQL to remove anything after the space and all leading zeros from the values so that the final output will be:
1
26
我如何最好地这样做?
Btw我正在使用DB2
Btw I'm using DB2
推荐答案
这是在DB2 for Linux / Unix / Windows和z / OS上进行了测试。
This was tested on DB2 for Linux/Unix/Windows and z/OS.
您可以使用 LOCATE()
函数在DB2中查找字符串中第一个空格的字符位置,然后发送到 SUBSTR()
作为结束位置(减一),只得到前n个字符串的数字。铸造到 INT
将摆脱前导零,但如果您需要字符串形式,您可以再次 CAST
到 CHAR
。
You can use the LOCATE()
function in DB2 to find the character position of the first space in a string, and then send that to SUBSTR()
as the end location (minus one) to get only the first number of the string. Casting to INT
will get rid of the leading zeros, but if you need it in string form, you can CAST
again to CHAR
.
SELECT CAST(SUBSTR(col, 1, LOCATE(' ', col) - 1) AS INT)
FROM tab
这篇关于删除前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!