Oracle 10g-快速查询

发布于 2024-09-08 05:18:35 字数 231 浏览 5 评论 0原文

我想对我的 oracle 数据库中的表执行特殊查询。

我希望结果根据我拥有的枚举进行排序。

枚举如下:

private enum days
{
    Saturday = 1,
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
}

我希望根据此枚举对结果进行排序。

I want to perform a special query on a table in my oracle database.

I want the result to be sorted according to an enum that I have.

the enum goes as follows:

private enum days
{
    Saturday = 1,
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
}

I want the result to be sorted according to this enum.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

烛影斜 2024-09-15 05:18:35

这听起来更像是一个 SQL 问题;以下参考可能会有所帮助:

http://www.techonthenet.com/oracle/functions/ to_date.php

暴力破解方法是在 SQL 中使用 case 语句;与您的代码片段等效的是:

SELECT CASE WHEN field1 = 'Saturday' then 1
            WHEN field1 = 'Sunday' then 2
            WHEN field1 = 'Monday' then 3
            ...
            END as dateval,
       field1 as datedisplay
  FROM table1
 ORDER BY 1;

DECODE SQL 函数也可以工作,但我喜欢 CASE 因为它更容易阅读并避免拼写错误或错误。

如果您从日期值中获得更多信息,我建议将数据库字段中的日期值完整保留为 DATE 数据类型。如果您需要找出星期几,使用上面的参考,您可以简单地将日期值转换为表示正确的、对应的星期几值的字符串。

测试:

SELECT to_char(SYSDATE, 'DAY') as day_of_week
  FROM dual;

产量:

WEDNESDAY
(or whatever day of the week is local to your system)

请记住,从 to_char 函数返回的任何内容都是区分大小写的。这应该适用于所有版本的 Oracle,包括 Oracle-XE。

This sounds more like a SQL question; The following reference may be helpful:

http://www.techonthenet.com/oracle/functions/to_date.php

The brute-force method is to use a case statement in your SQL; the equivallent to your snippet of code is:

SELECT CASE WHEN field1 = 'Saturday' then 1
            WHEN field1 = 'Sunday' then 2
            WHEN field1 = 'Monday' then 3
            ...
            END as dateval,
       field1 as datedisplay
  FROM table1
 ORDER BY 1;

The DECODE SQL function also works as well, but I like CASE because it's easier to read and avoid typos or mistakes.

If you have more information from your date values, I suggest to keep your date values intact in your database fields as a DATE data type. If you need to find out the day of the week, using the reference above, you can simply cast your date value as a character string representing the correct, corresponding day of the week value.

to test:

SELECT to_char(SYSDATE, 'DAY') as day_of_week
  FROM dual;

yields:

WEDNESDAY
(or whatever day of the week is local to your system)

Keep in mind that whatever is returned from the to_char function is case sensitive. This should work on all editions of Oracle, including Oracle-XE.

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