store in char column getting from decimal column in db2

…衆ロ難τιáo~ 提交于 2020-01-06 23:43:05

问题


create table test1 (no decimal(4,2) ,name char(10))

create table test2 (no char(1) ,name char(10))

insert into test1 values(1,'aa') insert into test1 values(2,'ab') insert into test1 values(3,'ac') insert into test1 values(4,'ad') insert into test1 values(null,'ad')

insert into test2 (no,name) (select cast(no as char(1)),name from test1)

not working. any clues.

Thanks.


回答1:


Are you intentionally using CHAR rather than VARCHAR? VARCHAR won't pad your text with spaces.

When I run the insert, I get the following error:

insert into test2 (no,name) (select cast(no as char(1)),name from test1)
SQL0445W  Value "1.00  " has been truncated.  SQLSTATE=01004

The actual insert works for me, but it suggests that you need CHAR(4) or VARCHAR(4) rather than CHAR(1).

If you want to automatically drop the digits after the decimal, you could cast the values to BIGINT first:

insert into test2 (no,name) (
  select 
    cast(cast(no as bigint) as char(1)),
    name
  from test1
)

Note that you will still run into truncation issues for values like 10 or -1.



来源:https://stackoverflow.com/questions/4191365/store-in-char-column-getting-from-decimal-column-in-db2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!