仅更新 PL/SQL 上的日期时间字段上的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3408906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:06:05  来源:igfitidea点击:

Update only date on datetime field on Pl/SQL

sqloracleplsql

提问by Gotjosh

So i need to update some dates on an Oracle Database, the field is a datetime, but i only want the date updated and leave the time as it is... There query goes like this:

所以我需要更新 Oracle 数据库上的一些日期,该字段是日期时间,但我只希望更新日期并将时间保持原样......查询如下:

update table 
   SET field = to_date('07312010','MMDDYY');

But it's overriding the hours, minutes and seconds from the field, i want to update the date but i want the hour to be left the same, any thoughts?

但它覆盖了现场的小时、分钟和秒,我想更新日期,但我希望小时保持不变,有什么想法吗?

回答by OMG Ponies

You could use:

你可以使用:

UPDATE TABLE
   SET field = TO_DATE('07312010' || ' ' || TO_CHAR(field, 'HH24:MI:SS'),
                       'MMDDYY HH24:MI:SS');

回答by Rory B. Bellows

In Oracle the blank is a minor issue , I modified it a little bit.

在 Oracle 中,空白是一个小问题,我对其进行了一些修改。

/* Formatted on 4/26/2017 5:56:31 AM (QP5 v5.115.810.9015) */

UPDATE  telco_attendee
   SET startdate =
TO_DATE(  (  TO_CHAR(startdate, 'DD/MM/YYYY')
|| 
TO_CHAR(starttime, 'HH24:MI:SS')  )  ,'DD/MM/YYYYHH24:MI:SS')