如何将LONG数据转换为TIMESTAMP或VARCHAR2?

时间:2012-03-10 04:24:31

标签: sql oracle oracle11g

我需要类似的内容:

SELECT PARTITION_NAME,
       to_char(LONG_TO_TIMESTAMP(HIGH_VALUE), 'MM/DD/YYYY HH24:MI:SS') AS HIGH_VAL
  FROM USER_TAB_PARTITIONS
 WHERE TABLE_NAME = 'TABLE_NAME'

输出应如下所示:

PARTITION_NAME         HIGH_VAL
----------------   --------------------
SOME_NAME          01/01/2010 00:00:00

我跑的时候:

  

SELECT PARTITION_NAME,来自USER_TAB_PARTITIONS的HIGH_VALUE

看起来像是:

PARTITION_NAME         HIGH_VAL
----------------   --------------------
SOME_NAME          TIMESTAMP' 2010-01-01 00:00:00'

如果我使用UTL_RAW.CAST_TO_VARCHAR2(HIGH_VALUE)
我最终收到ORA-00997: illegal use of LONG datatype错误。

如果我使用''||HIGH_VALUEto_clob(HIGH_VALUE)to_char(HIGH_VALUE),则 我收到ORA-00932: inconsistent datatypes: expected [DATA_TYPE] got LONG错误

我的工作功能归功于shobi:

CREATE OR REPLACE FUNCTION GET_HIGH_VALUE_AS_DATE (
    p_TableName     IN VARCHAR2,
    p_PatitionName  IN VARCHAR2
) RETURN DATE
IS
   v_LongVal LONG;
BEGIN
    SELECT HIGH_VALUE INTO v_LongVal
      FROM USER_TAB_PARTITIONS
     WHERE TABLE_NAME = p_TableName
       AND PARTITION_NAME = p_PatitionName;

    RETURN TO_DATE(substr(v_LongVal, 11, 19), 'YYYY-MM-DD HH24:MI:SS');
END GET_HIGH_VALUE_AS_DATE;

SQL看起来像

SELECT PARTITION_NAME, GET_HIGH_VALUE_AS_DATE(TABLE_NAME, PARTITION_NAME)
  FROM USER_TAB_PARTITIONS
 WHERE TABLE_NAME LIKE 'TABLE_NAME'
   AND ROWNUM < 2;

4 个答案:

答案 0 :(得分:6)

转换LONG列的唯一方法是使用PL / SQL。请看以下示例,该示例确定LONG字段的长度:

SET SERVEROUTPUT ON SIZE 10000;  
DECLARE
long_var LONG;
BEGIN
SELECT text_column INTO long_var
FROM table_with_long
WHERE rownum < 2;
DBMS_OUTPUT.PUT_LINE('The length is '||LENGTH(long_var));
END;

基本上,您将变量定义为LONG类型,然后选择INTO变量列。最后,它输出给用户。 SET SERVEROUTPUT ON SIZE 10000允许从PUT_LINE进行假脱机以进入屏幕。

您可以使用类似的方法将LONG选择到varchar字段中。以下示例将前2000个字符放入TABLE_B中,为了我们的目的,它有一列TEXT_FIELD:

DECLARE
   long_var LONG;
   var_var  VARCHAR2(2000);
   BEGIN
   SELECT text_column INTO long_var
   FROM table_with_long
   WHERE rownum < 2;
   var_var := substr(long_var,1,2000);
   INSERT INTO table_b
   VALUES (var_var);
   END;

答案 1 :(得分:1)

还可以执行以下操作:

CREATE TABLE long_to_clob
( partition_name VARCHAR2(30)
, high_value_clob CLOB
, high_value_text VARCHAR2(4000)
);

INSERT INTO long_to_clob (partition_name, high_value_clob)
SELECT partition_name, TO_LOB(high_value)
  FROM user_tab_partitions;

UPDATE long_to_clob
   SET high_value_text = DBMS_LOB.SUBSTR(high_value_clob, 1, 4000);

使用TO_LOB的唯一警告是必须在上面的INSERT语句中使用它。另一方面,使用DBMS_LOB包的限制较少。显然,一旦你在VARCHAR2列中获得了值,你可以用它做任何你想做的事。

希望这有帮助。

答案 2 :(得分:0)

使用动态sql(EXECUTE IMMEDIATE)有一个很好的技巧

SET SERVEROUTPUT ON
DECLARE
     l_date   DATE;
BEGIN
     FOR r IN (
          SELECT partition_name,high_value
            FROM user_tab_partitions
          WHERE table_name = 'MYTABLE'
     ) LOOP
          EXECUTE IMMEDIATE 'BEGIN :v := ' || r.high_value || '; END;' 
             USING  OUT l_date;
          DBMS_OUTPUT.PUT_LINE(r.partition_name||','|| l_date);
     END LOOP;
END;
/

这将产生以下形式的输出

SYS_P601452,19-10-18  --(partition_name,high_value)
SYS_P601451,18-10-18
SYS_P601453,20-10-18
SYS_P601454,21-10-18
SYS_P601455,22-10-18
..
..

在这里,我使用了DATE类型的分区列。您可以使用TIMESTAMP变量。

答案 3 :(得分:0)

SELECT 
  PARTITION_NAME,
  high_value
FROM XMLTABLE(
'/ROWSET/ROW'
PASSING
  DBMS_XMLGEN.GETXMLTYPE
    (Q'~
select
   p.partition_name,
   p.high_value
from user_tab_partitions p
 where table_name = 'MYTABLE'
order by partition_position
~'
)
COLUMNS PARTITION_NAME, HIGH_VALUE
);