且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除前导零

更新时间:2023-02-25 21:24:52

这是在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