gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:01:17 25 4
gpt4 key购买 nike

我需要类似的东西:

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

When I run:

SELECT PARTITION_NAME, HIGH_VALUE FROM USER_TAB_PARTITIONS

It looks like:

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

If I use UTL_RAW.CAST_TO_VARCHAR2(HIGH_VALUE),
I end up getting an ORA-00997: illegal use of LONG datatype error.

If I use ''||HIGH_VALUE or to_clob(HIGH_VALUE) or to_char(HIGH_VALUE),
I get an ORA-00932: inconsistent datatypes: expected [DATA_TYPE] got LONG error

My working function thanks to 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;

最佳答案

转换 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 类型,然后将列 SELECT 到变量中。最后输出给用户。 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;

关于sql - 如何将 LONG 数据转换为 TIMESTAMP 或 VARCHAR2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9643920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com