Oracle 日期函数

发布于 2024-08-09 05:09:32 字数 171 浏览 5 评论 0原文

我正在执行以下查询,它返回空白行。但是表中有 upd_time = '12-MAR-08' 的记录。我不明白为什么它不返回日期 '12-MAR-08 '.请帮帮我?

从 BANKREC.br_data 中选择 br_data.upd_time 其中 br_data.upd_time = '12-MAR-08';

I am executing the below query,It returns me the blank row.However there are records in the table having upd_time = '12-MAR-08'.I don't understand why it is not returning the date '12-MAR-08'.Please help me out??

SELECT br_data.upd_time FROM BANKREC.br_data
where br_data.upd_time = '12-MAR-08';

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

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

发布评论

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

评论(4

梦途 2024-08-16 05:09:32

upd_time 可能不完全是 2008 年 3 月 12 日。日期格式没有显示时间部分,但它可能存在(Oracle 中的 DATE 数据类型可以同时包含日期和时间部分)。

试试这个(它可以让你看到时间成分):

alter session set nls_date_format='DD-MON-YY HH24:MI:SS';

SELECT br_data.upd_time FROM BANKREC.br_data
where br_data.upd_time >= to_date('12-MAR-08','DD-MON-YY')
and br_data.upd_time < to_date('13-MAR-08','DD-MON-YY');

It's likely that upd_time isn't exactly 12-MAR-08. The date format is not showing the time component, but it's probably there (DATE data type in Oracle can contain both date and time components).

Try this (it will allow you to see the time components):

alter session set nls_date_format='DD-MON-YY HH24:MI:SS';

SELECT br_data.upd_time FROM BANKREC.br_data
where br_data.upd_time >= to_date('12-MAR-08','DD-MON-YY')
and br_data.upd_time < to_date('13-MAR-08','DD-MON-YY');
擦肩而过的背影 2024-08-16 05:09:32

如果您执行 a ,是否相同?

SELECT br_data.upd_time FROM BANKREC.br_data 
       where trunc(br_data.upd_time) = '12-MAR-08';

可能是 upd_time 不是日期,而是时间戳,因此它实际上包含例如“12-MAR-08 05:30”,这不是同一件事。 trunc() 删除时间戳的时间部分。

由于它不会返回错误,我假设它正确解析了日期,但否则您可以尝试使用 to_date('12-MAR-08','DD-MON-YY')

Is it the same if you do a

SELECT br_data.upd_time FROM BANKREC.br_data 
       where trunc(br_data.upd_time) = '12-MAR-08';

It could be that the upd_time is not a date, but a timestamp, so it actually contains for instance '12-MAR-08 05:30' which wouldn't be the same thing. trunc() removes the time part of the timestamp.

Since it doesn't return an error, I assume that it parses the date correctly, but otherwise you could try with to_date('12-MAR-08','DD-MON-YY')

望她远 2024-08-16 05:09:32

您应该使用 Oracle 的函数将字符串正确转换为日期,

to_date('12-MAR-08', 'DD-MMM-YY')

然后您必须考虑到 Oracle“日期”数据类型还包含精确到秒的时间信息。这意味着第一步构建的日期实际上是 3 月 12 日午夜。因此,您必须确保 upd_time 被截断为午夜:

trunc(upd_time, 'DAY') = to_date('12-MAR-08', 'DD-MMM-YY')

您的完整查询变成

SELECT br_data.upd_time 
FROM BANKREC.br_data 
WHERE trunc(upd_time, 'DAY') = to_date('12-MAR-08', 'DD-MMM-YY');

There are other way to Skin this cat (您可以使用 to_char(upd_time, 'DD-MMM) 将 updTime 列转移到正确的 char 字段-YY')),但通常建议您查找的数据与数据库中可以找到的数据相似,因为这会增加使用索引进行查找的机会。

You should use Oracle's function to convert your string properly into a date using

to_date('12-MAR-08', 'DD-MMM-YY')

Then you have to take into account that the Oracle "Date" datatype also contains time information to the nearest second. This means that the date that was constructed in the first step is actually midnight on March 12th. So you have to make sure that the upd_time is truncated to midnight:

trunc(upd_time, 'DAY') = to_date('12-MAR-08', 'DD-MMM-YY')

Your full query becomes

SELECT br_data.upd_time 
FROM BANKREC.br_data 
WHERE trunc(upd_time, 'DAY') = to_date('12-MAR-08', 'DD-MMM-YY');

There are other ways to skin this cat (you could transfer your updTime column to a proper char field with to_char(upd_time, 'DD-MMM-YY')), but it's usually advisable make the data you are looking for similar to what you can find in the database as that increases your chances of using an index for the lookup.

弄潮 2024-08-16 05:09:32

我目前无法访问 oracle 数据库,但我记得使用 to_char。

尝试

SELECT br_data.upd_time FROM BANKREC.br_data where to_char(br_data.upd_time, 'DD-MON-YY') = '12-MAR-08';

i don't have access to an oracle db at the moment but i remember using to_char.

try

SELECT br_data.upd_time FROM BANKREC.br_data where to_char(br_data.upd_time, 'DD-MON-YY') = '12-MAR-08';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文