存储在char列中,从db2的十进制列开始 [英] store in char column getting from decimal column in db2

查看:329
本文介绍了存储在char列中,从db2的十进制列开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建表test1(无小数(4,2),名称char(10))



创建表test2(无char(1),名称char )

$ b插入test1值(1,'aa')
插入test1值(2,'ab')
插入test1值(3,'ac')
插入test1值(4,'ad')
插入test1值(null,'ad')



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



加工。任何线索。



谢谢。

解决方案

而不是VARCHAR? VARCHAR不会用空格填充你的文本。



当我运行插入,我得到以下错误:

  insert into test2(no,name)(select cast(no as char(1)),name from test1)
SQL0445W值1.00已被截断。 SQLSTATE = 01004

实际插入对我有用,但建议您需要CHAR(4)或VARCHAR(4)而不是CHAR(1)。



如果要自动删除小数后的数字,可以先将值转换为BIGINT:

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

注意您仍然会遇到10或-1值的截断问题。


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.

解决方案

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.

这篇关于存储在char列中,从db2的十进制列开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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