2

我的数据库表中有数据

last_updated_product
01/Jan/1899 6:25:01 AM
01/Jan/1899 6:25:02 AM

如何在不修改时间部分的情况下仅使用 sysdate 更新日期部分?预期输出

21/Aug/2013 6:25:01 AM
21/Aug/2013 6:25:02 AM

last_updated_product列数据类型定义为date

谢谢

4

3 回答 3

5

You need to use midnight on the current day, and add on the time part from the original value:

trunc(sysdate) + (last_updated_product - trunc(last_updated_product))

trunc() gives you the date with the time component set to 00:00:00, so date - trunc(date) gives you just the original time component, as a number (fraction of a day) as per the datetime arithmetic rules. That number can then be added on to midnight today.

Not sure if you're actually updating the table or just doing this in a query, but it's the same calculation either way.

于 2013-08-21T07:16:59.200 回答
2

您可以计算出时间部分并添加所需的日期,例如:

update my_table
   set last_updated_product = 
              to_date('21/Aug/2013', 'dd/Mon/yyyy')
              -- difference between the start of the day and the time
              + (last_updated_product - trunc(last_updated_product))

额外的括号是为了确保查询按照操作员的优先顺序工作,因为您不能将日期添加到日期,但您可以添加间隔。括号确保last_updated_product - trunc(last_updated_product)在添加之前进行评估。

或将其转换为字符,将其连接到日期,然后将其转换回日期。

update my_table
   set last_updated_product = 
             to_date('21/Aug/2013' || to_char(last_updated_product, 'hh24:mi:ss')
                     , 'dd/Mon/yyyyhh24:mi:ss')

例如

create table my_table ( last_updated_product date );

Table created.

insert into my_table values (sysdate - 100);

1 row created.

update my_table
   set last_updated_product =
          to_date('21/Aug/2013', 'dd/Mon/yyyy')
          + (last_updated_product - trunc(last_updated_product))
       ;

1 row updated.

select * from my_table;

LAST_UPDATED_PRODUC
-------------------
2013/08/21 08:13:57
于 2013-08-21T07:12:21.273 回答
0

尝试

   update <table>
      set last_updated_product = 
               last_updated_product 
             - trunc(last_updated_product )
             + trunc(sysdate)
    where <condition>
        ;
于 2013-08-21T07:17:53.453 回答