db2中的pad varchar数字为0 [英] pad varchar numbers with 0s in db2

查看:148
本文介绍了db2中的pad varchar数字为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如下所示:

  some_column result 
----------- ------
12 ==> 00012
123 ==> 00123
6454 ==> 06454


解决方案

如果函数 LPAD 可用:

  SELECT LPAD(some_column,5,'0')
FROM table

否则,您可以使用 RIGHT REPEAT

  SELECT RIGHT(REPEAT('0',5)|| some_column,5)
FROM table

some_column |将五个0连接到some_column |返回五个最右边的角色
----------------------------------------- -------------------------------
12 => 0000012 => 00012
123 => 00000123 => 00123
6454 => 000006454 => 06454


Is there a way to pad 0s before numbers stored as VARCHAR in DB2?

Like this:

some_column     result
-----------     ------
12          ==>  00012
123         ==>  00123
6454        ==>  06454

解决方案

If the function LPAD is available:

SELECT LPAD(some_column, 5, '0')
FROM table

Otherwise you can use a combination of RIGHT and REPEAT:

SELECT RIGHT(REPEAT('0', 5) || some_column, 5)
FROM table

some_column  |  Concatenate five '0's to some_column  | Return the five rightmost characters
------------------------------------------------------------------------
    12       =>             0000012                   =>   00012
   123       =>            00000123                   =>   00123
  6454       =>           000006454                   =>   06454

这篇关于db2中的pad varchar数字为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆