简体   繁体   中英

Expected TIMESTAMP got NUMBER error

I have two columns, one is NUMBER_COLUMN(with number datatype) and the other is a TIMESTAMP_COLUMN(with timestamp datatype) in different tables. I need to convert the number_column values into TIMESTAMP data format so I can validate their values. I got a working code for the conversion--see below.

CAST(LAST_DAY(to_timestamp(lpad(cast(NUMBER_COLUMN as varchar2(6)), 6, '0'), 'MMYYYY')) AS TIMESTAMP)

I use the above query in a select statement to convert the NUMBER_column data value into the format that corresponds with my timestamp_column data format. Then I have a NOT EXISTS block that is basically another select statement that ends with where NUMBER_COLUMN=TIMESTAMP_COLUMN. But this last condition gives me an error "TIMESTAMP GOT NUMBER". See below for the script.

SELECT CAST(LAST_DAY(to_timestamp(lpad(cast(NUMBER_COLUMN as varchar2(6)), 6, '0'), 'MMYYYY')) AS TIMESTAMP) AS NUMBER_COLUMN FROM TAB1 T1
WHERE NOT EXISTS(SELECT P.TIMESTAMP_COLUMN FROM 
                 (SELECT TIMESTAMP_COLUMN FROM TAB2)
                 WHERE P.TIMESTAMP=T1.NUMBER_COLUMN);

I think the problem is that your alias and column name are the same, so when you refer to t1.number_column it is understanded as the actual number column, not the calculated timestamp value. In addition, you can not use alias of the column in the correlated subquery. So, this should work:

SELECT CAST(LAST_DAY(to_timestamp(lpad(cast(NUMBER_COLUMN as varchar2(6)), 6, '0'), 'MMYYYY')) AS TIMESTAMP) AS NUMBER_COLUMN_ALIAS FROM TAB1 T1
WHERE NOT EXISTS(SELECT TIMESTAMP_COLUMN FROM 
                 (SELECT TIMESTAMP_COLUMN FROM TAB2)
                 WHERE TIMESTAMP_COLUMN=CAST(LAST_DAY(to_timestamp(lpad(cast(NUMBER_COLUMN as   varchar2(6)), 6, '0'), 'MMYYYY')) AS TIMESTAMP));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM