Oracle - get current date formatted

血红的双手。 提交于 2019-12-13 09:18:05

问题


I've been wondering - I have a lot of use with sysdate on my system, and when comparing it to my date columns I have to use trunc(sysdate) since the format of sysdate is DD/MM/YYYY HH24:MI:SS .

I looked over the internet for other functions to return current date on format of DD/MM/YYYY but had no luck, current_date , current_timestamp and ETC also gives me the hours format..

I need this for better performance when comparing an indexed date column to the current date.

So , anybody know of a system function that returns the desired format? or a way to bypass it?


回答1:


If your "date" column is of type DATE, then you do not need to be concerned with "format". You need to compare trunc(column_name)to trunc(sysdate). And if that is causing serious performance issues because the use of a function eliminates the use of the index, then create a function-based index. A less elegant solution would be

WHERE mydatecol > trunc(sysdate) AND mydatecol < trunc(sysdate +1)

What you do NOT want to do is confuse the binary concept of DATE with the character string representation of a date. A character string is just a string of characters that you as a human recognize as a data, but to the computer, '2016-02-14' has no more meaning than 'hereisyoursign'.




回答2:


you may change the variable NLS_DATE_FORMAT.

ALTER SESSION SET NLS_DATE_FORMAT = 'DD/MM/YYYY';

see also http://www.dba-oracle.com/t_nls_date_format.htm



来源:https://stackoverflow.com/questions/35389826/oracle-get-current-date-formatted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!