Oracle - 将日期值转换为 TO_CHAR()

发布于 2024-12-14 00:32:06 字数 296 浏览 3 评论 0原文

好的,所以我想转换日期值以正确格式化我的日期以进行比较,但是,将我的“DATE”数据类型转换为 char 会影响在该字段上进行比较时的索引吗?我可以通过执行 to_date(tochar()) 来解决这个问题吗?任何对此的建议将不胜感激,谢谢!

编辑 - 抱歉缺乏细节......基本上我需要消除日期中的时间戳,我使用了以下内容,它似乎可以工作 TO_DATE(TO_CHAR, 'YYYY-MM-DD '),'YYYY-MM-DD'),请注意,我不知道这是否是一个好的做法,但至少(或者我认为)现在它正在将日期与日期进行比较,而不是字符串。

Ok, so I want to convert a date value to properly format my date for comparison, however, will converting my "DATE" data type to char affect indexing when comparing on that field? Can I resolve this by doing to_date(tochar())? Any advice on this would be greatly appreciated Thanks!

EDIT - Sorry for the lack of specifics.... basically I need to eliminate the time stamp from my date, I used the following and it appears to work TO_DATE(TO_CHAR, 'YYYY-MM-DD'),'YYYY-MM-DD'), mind you I don't know if this is good practice or not, but at least (or so I think) now it's comparing a DATE against a DATE, and not a string.

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

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

发布评论

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

评论(3

几味少女 2024-12-21 00:32:06

如果您正在进行比较,则不应将日期转换为字符串。您应该与另一个日期进行比较。否则,Oracle 将无法在日期列上使用非基于函数的索引。

一般来说,也就是说,您最好编码

WHERE some_indexed_date_column = to_date( :string_bind_variable, 
                                          <<format mask>> )

而不是

WHERE to_char( some_indexed_date_column, 
               <<format mask>> ) = :string_bind_variable

当然,如​​果您的绑定变量可以是 DATE 而不是 VARCHAR2,那就更好了,因为然后您不必进行任何数据类型转换,优化器可以更轻松地估计基数。

如果您尝试对日期进行一些操作(例如,如果您想比较日期部分而忽略日期的时间部分),您可能需要使用基于函数的索引。例如,如果您想查找今天某个时间创建的所有行,

WHERE trunc( some_date_column ) = date '2011-11-04'

您可以在日期列上创建基于函数的索引

CREATE INDEX idx_trunc_dt
    ON table_name( trunc( some_date_column ) )

,也可以重写查询以执行类似的操作

WHERE some_date_column >= date '2011-11-04'
  AND some_date_column <  date '2011-11-05'

If you are doing a comparison, you should not be converting the date to a string. You should be comparing to another date. Otherwise, Oracle won't be able to use a non function-based index on the date column.

In general, that is, you're much better off coding

WHERE some_indexed_date_column = to_date( :string_bind_variable, 
                                          <<format mask>> )

rather than

WHERE to_char( some_indexed_date_column, 
               <<format mask>> ) = :string_bind_variable

Of course, if your bind variable can be a DATE rather than a VARCHAR2, that's even better because then you don't have to do any data type conversion and the optimizer has a much easier time of estimating cardinalities.

If you are trying to do some manipulation of the date-- for example, if you want to compare the day portion while omitting the time portion of the date-- you may want to use function-based indexes. For example, if you wanted to find all the rows that were created some time today

WHERE trunc( some_date_column ) = date '2011-11-04'

you could either create a function-based index on the date column

CREATE INDEX idx_trunc_dt
    ON table_name( trunc( some_date_column ) )

or you could rewrite the query to do something like

WHERE some_date_column >= date '2011-11-04'
  AND some_date_column <  date '2011-11-05'
幽梦紫曦~ 2024-12-21 00:32:06

您应该将日期作为日期进行比较,而不是作为字符串进行比较。如果将日期与字符串进行比较,请将字符串转换为日期进行比较。

You should compare dates as dates, not as strings. If comparing a date to a string, convert the string to a date to compare.

小伙你站住 2024-12-21 00:32:06

IMO,您应该将比较值转换为数据库中存储的格式。否则,您将需要在 DATE 列上创建基于函数的索引才能利用索引。因此,如果您输入的字符日期为 11/4/2011,您可以在 where 子句中对其进行比较:

SELECT ...
  FROM your_table
 WHERE the_date_column = TO_DATE('11/4/2011','MM/DD/YYYY');

IMO, you should convert your comparison value to the format stored in the database. Otherwise, you will need to create a function based index on the DATE column to take advantage of indexing. So, if you have an input character date of, say, 11/4/2011, you could compare it in a where clause thusly:

SELECT ...
  FROM your_table
 WHERE the_date_column = TO_DATE('11/4/2011','MM/DD/YYYY');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文