Oracle 10g:CAST到数据类型CHAR导致前导空格?

时间:2014-09-23 19:15:19

标签: oracle plsql casting whitespace database-migration

首先,视图中的CAST(COLUMN_NAME为CHAR(XX))和TO CHAR(COLUMN_NAME)之间有什么区别?

我遇到的问题是我们正处于Oracle to SQL Server转换过程中,并且在表的列上需要进行数据类型转换。所以我使用cast函数制作了一个视图:

CREATE OR REPLACE VIEW ARS_DMIS AS
SELECT
CAST (DMIS_TBL_ID AS CHAR(15)) "DMIS_TBL_ID",
CAST (PARENT_DMIS_CODE AS CHAR(4)) "PARENT_DMIS_CODE",
CAST (CHILD_DMIS_CODE AS CHAR(4)) "CHILD_DMIS_CODE",
CAST (SERVICE AS CHAR(15)) "SERVICE",
CAST (TYPE_FACILITY AS CHAR(10)) "TYPE_FACILITY",
CAST (UNIT_ORGANIZATION AS CHAR(60)) "UNIT_ORGANIZATION",
CAST (SITE AS CHAR(60)) "SITE",
CAST (LOCATION AS CHAR(60)) "LOCATION",
CAST (CITY AS CHAR(30)) "CITY",
CAST (STATE AS CHAR(2)) "STATE",
CAST (ZIP AS CHAR(10)) "ZIP",
CAST (DODAAC AS CHAR(10)) "DODAAC",
CAST (REGION AS CHAR(10)) "REGION",
CAST (CONUS AS CHAR(3)) "CONUS",
CAST (POC AS CHAR(30)) "POC",
CAST (POC_PHONE_NUMBER AS CHAR(20)) "POC_PHONE_NUMBER",
TO_CHAR (LAST_DATE, 'MM/DD/YYYY HH24:MI:SS') "LAST DATE",
CAST (LAST_USER AS CHAR(32)) "LAST_USER",
CAST (EMAIL AS CHAR(255)) "EMAIL",
CAST (INVENTORY_SITE AS CHAR(11)) "INVENTORY_SITE",
CAST (INVENTORY_REGION_ID AS CHAR(15)) "INVENTORY_REGION_ID",
CAST (ROWID AS CHAR(20)) "ROWID"
FROM ASSET_MGT.DMIS_TBL;

我使用过的唯一一个例子" TO_CHAR"是因为我需要改变日期的格式而且铸造不允许这样做。

我将其他列转换为适当的数据类型以便迁移到SQL Server,并指定字符长度以匹配表中列的原始字符长度。但我刚刚发现,这样做会导致空格一直跟踪到指定的字符长度。我怎样才能防止这种情况?我知道我可以在视图查询中修改它:

SELECT TRIM (TRAILING FROM DMIS_TBL_ID) "DMIS_TBL_ID" from ARS_DMIS_TBL 

我不想走这条路,因为对所有列执行此操作会非常耗时。我起诉演员的功能错了吗?什么是防止这些白色空间填充的最有效方法?

1 个答案:

答案 0 :(得分:2)

CHAR(xx)是固定宽度的数据类型。根据定义,它必须以空格填充到指定的长度。

VARCHAR2(xx)是一种可变宽度数据类型。根据您的说明,您似乎确实想要castVARCHAR2(xx)而不是CHAR(xx)

话虽如此,将表中的所有列转换为字符串作为转换的一部分似乎很不寻常。当然,目标SQL Server数据库将使用datedatetime列来存储日期,它将使用数字数据类型来存储数字等。