Oracle - 将日期值转换为 TO_CHAR()
好的,所以我想转换日期值以正确格式化我的日期以进行比较,但是,将我的“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您正在进行比较,则不应将日期转换为字符串。您应该与另一个日期进行比较。否则,Oracle 将无法在日期列上使用非基于函数的索引。
一般来说,也就是说,您最好编码
而不是
当然,如果您的绑定变量可以是
DATE
而不是VARCHAR2
,那就更好了,因为然后您不必进行任何数据类型转换,优化器可以更轻松地估计基数。如果您尝试对日期进行一些操作(例如,如果您想比较日期部分而忽略日期的时间部分),您可能需要使用基于函数的索引。例如,如果您想查找今天某个时间创建的所有行,
您可以在日期列上创建基于函数的索引
,也可以重写查询以执行类似的操作
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
rather than
Of course, if your bind variable can be a
DATE
rather than aVARCHAR2
, 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
you could either create a function-based index on the date column
or you could rewrite the query to do something like
您应该将日期作为日期进行比较,而不是作为字符串进行比较。如果将日期与字符串进行比较,请将字符串转换为日期进行比较。
You should compare dates as dates, not as strings. If comparing a date to a string, convert the string to a date to compare.
IMO,您应该将比较值转换为数据库中存储的格式。否则,您将需要在 DATE 列上创建基于函数的索引才能利用索引。因此,如果您输入的字符日期为 11/4/2011,您可以在 where 子句中对其进行比较:
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: