比较第二个不同塔布拉斯的不同细节
晚上好,
我有一个 SP,我想比较不同表中的 2 个日期,但以“dd/mm/yyyy hh:mi:ss”的形式,
我使用 to_char(date01,'dd/mm/yyyy hh:mi :ss')> to_char(date02,'dd/mm/yyyy hh:mi:ss')
但它会引发错误。
例如:如果日期是 02/12/2016 07:40:12>02/02/2022 06:40:46
则表明这是真的,而事实并非如此,它考虑的是当天而不是整个日期。
当我只使用 date01>date02 时,我遇到了您考虑的问题。
'02/15/2022 07:48:50'='02/15/2022 07:50:22'(仅考虑日期)
无论服务器配置如何,如何比较日期、分钟和秒。
谢谢你,
PROCEDURE SPU_CUENTA
(
p_nro in varchar2,
pr_Ret OUT number
) is
vfecha varchar(100);
vcount int;
begin
select COUNT(DFEC_SISTEMA) into vcount from TAB Where c=1;
IF vcount>0 THEN
select to_char(DFEC_SISTEMA,'dd/mm/yyyy hh:mi:ss') into vfecha from TAB Where c=1;
EXECUTE IMMEDIATE 'SELECT COUNT(DFEC_ANULA) FROM tablab WHERE to_char(DFEC_ANULA,'dd/mm/yyyy hh:mi:ss')>'''||vfecha||'''' into pr_Ret;
END IF;
end;
Good evening,
I have a SP and I want to compare 2 dates from different tables, but in the form 'dd/mm/yyyy hh:mi:ss'
I am using to_char(date01,'dd/mm/yyyy hh:mi:ss')> to_char(date02,'dd/mm/yyyy hh:mi:ss')
but it throws me errors.
For example: if the date is 02/12/2016 07:40:12>02/02/2022 06:40:46
it indicates that it is true, and it is not, it is considering the day and not the entire date.
when I only use date01>date02, I have the problem you consider for example.
'02/15/2022 07:48:50'='02/15/2022 07:50:22' (only considers the date)
How can I compare date, minutes and seconds regardless of the server configuration.
Thank you,
PROCEDURE SPU_CUENTA
(
p_nro in varchar2,
pr_Ret OUT number
) is
vfecha varchar(100);
vcount int;
begin
select COUNT(DFEC_SISTEMA) into vcount from TAB Where c=1;
IF vcount>0 THEN
select to_char(DFEC_SISTEMA,'dd/mm/yyyy hh:mi:ss') into vfecha from TAB Where c=1;
EXECUTE IMMEDIATE 'SELECT COUNT(DFEC_ANULA) FROM tablab WHERE to_char(DFEC_ANULA,'dd/mm/yyyy hh:mi:ss')>'''||vfecha||'''' into pr_Ret;
END IF;
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果涉及的列是
VARCHAR2
(这是一个坏主意;将日期存储到DATE
数据类型列),您建议的代码将有意义。如果这些列确实是
DATE
,那么您的部分问题(建议格式)就没有意义 - 我们按原样比较日期,只需通过例如date1 >;日期2。将它们转换为字符(采用您指定的格式)是完全错误的。
如果这些列是字符串,那么您必须将它们转换为
TO_DATE
,而不是TO_CHAR
。您编写的过程应该是一个函数;它们被设计为返回一个值。是的,您可以使用程序,但是 - 为什么要这样做呢?您不能在 SQL 中使用它(仅在 PL/SQL 中)。
此外,代码可以大大缩短/优化,因为您只需要一个
select
语句。您不必首先检查tab
中是否有满足条件的行,然后选择其他一些信息 - 使用子查询即可。最后,为什么要使用动态 SQL?您的代码中没有任何动态内容。
我建议这样,看看是否有意义。
Code you suggest would make sense if columns involved were
VARCHAR2
(which is a bad idea; store dates intoDATE
datatype columns).If those columns really are
DATE
s, then part of your question (which suggests format) is meaningless - we compare dates as they are, simply by e.g.date1 > date2
. Converting them to characters - in a format you specified - is plain wrong.If those columns are strings, then you'll have to convert them
TO_DATE
, notTO_CHAR
Procedure you wrote should be a function; they are designed to return a value. Yes, you can use a procedure, but - why would you? You can't use it in SQL (only in PL/SQL).
Besides, code can be heavily shortened/optimized, as you need just one
select
statement. You don't have to first check whether there any rows intab
that satisfy the condition, and then select some other info - use a subquery instead.Finally, why are you using dynamic SQL? There's nothing dynamic in your code.
I'd suggest something like this, see if it makes sense.