比较第二个不同塔布拉斯的不同细节

发布于 2025-01-11 03:56:38 字数 885 浏览 0 评论 0原文

晚上好,

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懵少女 2025-01-18 03:56:38

如果涉及的列是 VARCHAR2(这是一个坏主意;将日期存储到 DATE 数据类型列),您建议的代码将有意义。

  • 如果这些列确实是DATE,那么您的部分问题(建议格式)就没有意义 - 我们按原样比较日期,只需通过例如 date1 >;日期2。将它们转换为字符(采用您指定的格式)是完全错误的。


  • 如果这些列是字符串,那么您必须将它们转换为TO_DATE,而不是TO_CHAR

。您编写的过程应该是一个函数;它们被设计为返回一个值。是的,您可以使用程序,但是 - 为什么要这样做呢?您不能在 SQL 中使用它(仅在 PL/SQL 中)。

此外,代码可以大大缩短/优化,因为您只需要一个 select 语句。您不必首先检查 tab 中是否有满足条件的行,然后选择其他一些信息 - 使用子查询即可。

最后,为什么要使用动态 SQL?您的代码中没有任何动态内容。

我建议这样,看看是否有意义。

FUNCTION spu_cuenta (p_nro IN VARCHAR2)
   RETURN NUMBER
IS
   pr_ret  NUMBER;
BEGIN
   SELECT COUNT (dfec_anula)
     INTO pr_ret
     FROM tablab
    WHERE dfec_anula > (SELECT dfec_sistema
                          FROM tab
                         WHERE c = 1);

   RETURN pr_ret;
END;

Code you suggest would make sense if columns involved were VARCHAR2 (which is a bad idea; store dates into DATE datatype columns).

  • If those columns really are DATEs, 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, not TO_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 in tab 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.

FUNCTION spu_cuenta (p_nro IN VARCHAR2)
   RETURN NUMBER
IS
   pr_ret  NUMBER;
BEGIN
   SELECT COUNT (dfec_anula)
     INTO pr_ret
     FROM tablab
    WHERE dfec_anula > (SELECT dfec_sistema
                          FROM tab
                         WHERE c = 1);

   RETURN pr_ret;
END;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文