简体   繁体   English

动态SQL查询To_Date格式Oracle

[英]Dynamic SQL Query To_Date Format Oracle

I am using Oracle SQL Developer. 我正在使用Oracle SQL Developer。

My tutor has asked us to include Dynamic Queries into our SQL statement. 我的老师要求我们在SQL语句中包含动态查询。

Part of my query is this: 我的查询部分是这样的:

where booking.date_event != to_date('20140309','yyyymmdd') and booking.occassion_id=2

I have modified the query one step at a time to include the dynamic aspect. 我一次修改了查询以包含动态方面。 So it now looks like this: 所以现在看起来像这样:

where booking.date_event != to_date('20140309','yyyymmdd') and booking.occassion_id='&occassion_id'

This gives me the pop-up box to enter the ID correctly and it works correctly. 这给了我一个弹出框,可以正确输入ID,并且可以正常工作。

However, i now want to do the same for the date. 但是,我现在想对日期做同样的事情。 But obviously entering a date in the format 'yyyymmdd' is not very user friendly. 但是很显然,以“ yyyymmdd”格式输入日期不是很友好。

How can i change my query to either allow for various types of date format or to add a message to the pop up box to inform the user to use the correct format? 如何更改查询以允许使用各种类型的日期格式或在弹出框中添加消息以通知用户使用正确的格式? At the moment the pop up box only says "Date_Event" and an input box. 目前,弹出框仅显示“ Date_Event”和一个输入框。

try to_date function. 尝试to_date函数。 Here is some tips for to_date function. 这是to_date函数的一些技巧。

http://www.dba-oracle.com/f_to_date.htm http://www.dba-oracle.com/f_to_date.htm

SQL Developer is no tool for users. SQL Developer不是面向用户的工具。 So you don't really have to worry about '20140309' format not being user friendly. 因此,您不必担心'20140309'格式不易于使用。 For real users you would write a real app that accepts a date (with a calendar popup for instance) and sends it to the database in appropriate format. 对于真正的用户,您将编写一个接受日期(例如带有日历弹出窗口)的真实应用程序,并将其以适当的格式发送到数据库。

However to have it a bit more convenient in SQL Developer, you could use to_date without format. 但是,为了使它在SQL Developer中更加方便,可以使用不带格式的to_date。 Having set your environment and the database to the same locale, you could simply enter a date in the format you are used to (eg 9.3.13 or 09.03.2013 or March 9, 2013) and it should all work. 将环境和数据库设置为相同的语言环境后,您只需以惯用的格式输入日期(例如9.3.13或09.03.2013或2013年3月9日),它就可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM