在导出数据价格值的db2中,我得到这样的结果
&#34 + 00000000000000000000000020.96000"
如何删除之前的价格零值。
我不喜欢这样" 20.96000" 。
我的查询
sum(orders.TOTALPRODUCT) this value I am getting like this "+00000000000000000000000020.96000"
db2 "EXPORT TO customerpurchase2016.csv OF del modified BY coldel,
SELECT userreg.logonid,
nvl(address.firstname,'')
||' '
|| address.lastname,
sum(orders.totalproduct) AS totalproduct,
sum(orders.totalproduct+orders.totaltax+orders.totalshipping+orders.totaladjustment) AS totalamount,
userdemo.field7
FROM orders,
userreg,
userdemo,
address
WHERE address.member_id=userdemo.users_id
AND userdemo.users_id=userreg.users_id
AND userreg.users_id=orders.member_id
AND orders.status NOT IN('J',
'P',
'X')
AND orders.lastupdate BETWEEN '2016-01-01-00.00.00.000000' AND '2016-12-30-12.00.00.000000'
AND address.status='P'
AND address.selfaddress=1
GROUP BY userreg.logonid,
userdemo.field7,
address.firstname,
address.lastname"
答案 0 :(得分:1)
我无法测试,但尝试使用cast( sum(orders.TOTALPRODUCT) as decimal( 31, 6 )
代替OP中的sum(orders.TOTALPRODUCT)
。如果前导零持续存在,那么可能正在使用的db2是LUW [没有提到平台,也没有提到OP中的版本信息也没有标签]并且[显式CHAR和可能还有]隐式转换从数字到字符,其中已知前导零是过去缺陷的结果;即,较旧的数据库可能需要修改才能使转换在DB2 for Linux UNIX and Windows 10.1.0->Database fundamentals->SQL->Built-in functions->Scalar functions->CHAR
十进制到字符和前导零:在版本9.7之前的版本中,此函数的十进制输入结果包括前导零和尾随十进制字符。数据库配置参数dec_to_char_fmt可以设置为“V95”,以使此函数返回小数输入的版本9.5结果。新数据库的dec_to_char_fmt的默认值为“NEW”,该函数返回的结果与SQL标准转换规则匹配,并与VARCHAR函数的结果一致。
答案 1 :(得分:0)
导出表时,您可以使用VARCHAR
或TRIM
OR STRIP
函数,如下所示;
db2 "EXPORT TO customerpurchase2016.csv OF del modified BY coldel,
SELECT userreg.logonid,
nvl(address.firstname,'')
||' '
|| address.lastname,
TRIM(sum(orders.totalproduct)) AS totalproduct,
sum(orders.totalproduct+orders.totaltax+orders.totalshipping+orders.totaladjustment) AS totalamount,
userdemo.field7
FROM orders,
userreg,
userdemo,
address
WHERE address.member_id=userdemo.users_id
AND userdemo.users_id=userreg.users_id
AND userreg.users_id=orders.member_id
AND orders.status NOT IN('J',
'P',
'X')
AND orders.lastupdate BETWEEN '2016-01-01-00.00.00.000000' AND '2016-12-30-12.00.00.000000'
AND address.status='P'
AND address.selfaddress=1
GROUP BY userreg.logonid,
userdemo.field7,
address.firstname,
address.lastname"
实施例;
样本表:
create table pricetable (price decimal(31,5));
insert into pricetable (price) values(20.96);
insert into pricetable (price) values(21.97);
insert into pricetable (price) values(22.98);
insert into pricetable (price) values(20.99);
导出无功能;
CALL SYSPROC.ADMIN_CMD( 'EXPORT TO "/tmp/export" OF DEL MESSAGES ON SERVER SELECT PRICE FROM PRICETABLE' );
user@host:/tmp:>cat export
+00000000000000000000000020.96000
+00000000000000000000000021.97000
+00000000000000000000000022.98000
+00000000000000000000000020.99000
使用varchar或trim或strip函数导出;
CALL SYSPROC.ADMIN_CMD( 'EXPORT TO "/tmp/export" OF DEL MESSAGES ON SERVER SELECT VARCHAR(PRICE) FROM PRICETABLE' );
user@host:/tmp:>cat export
"20.96000"
"21.97000"
"22.98000"
"20.99000"
答案 2 :(得分:0)
您可以通过简单地将striplzeros
作为参数来告诉export-script删除所有列中的前导零。
db2 "EXPORT TO customerpurchase2016.csv OF del modified BY coldel, striplzeros
SELECT userreg.logonid,
nvl(address.firstname,'')
...