Print Thread
querying for dates by text
#56855 08/02/17 03:29 PM
Joined: Aug 2017
Posts: 4
D
Member
OP Offline
Member
D
Joined: Aug 2017
Posts: 4
when I still used PL/SQL Developer in Version 11.x , I could write queries like:

select * from my_tab t where t.day = '01.01.2017';

Now with Version 12.x this doesn't work anymore, I get an error saying invalid month. I tried different literals like '2017-01-01' but no luck.

Is there any way to activate this helpful Feature in Version 12.x so I can write queries in this abbreviated form?

Re: querying for dates by text
Dirk_1234 #56857 08/02/17 06:45 PM
Joined: Feb 2016
Posts: 80
Ontario, Canada
Member
Offline
Member
Joined: Feb 2016
Posts: 80
Ontario, Canada
try the standard Oracle Date literal:

select * from my_tab t where t.day = DATE '2017-01-01';

Keyword DATE follow by quoted string format 'YYYY-MM-DD'

This work regardless of NLS language or date format, in PL/SQL Dev (any version), SQL*Plus, everywhere.

Last edited by Nicolas D.; 08/02/17 06:47 PM.
Re: querying for dates by text
Nicolas D. #56866 08/03/17 01:44 PM
Joined: Aug 2017
Posts: 4
D
Member
OP Offline
Member
D
Joined: Aug 2017
Posts: 4
thanks, I didn't know about this way to do it. But I'm still very sad that the old and abbreviated way doesn't work anymore.

But one more question: How can I use a different date format? That used to work out of the box in the old PL/SQL-Developer version.

When I display dates, it all works fine, they get displayed in the german format. (Settings -> NLS Options -> Date is by default the german format for me). But the SQL Editor seems to only accept the YYYY-MM-DD format.

Re: querying for dates by text
Nicolas D. #56867 08/03/17 01:51 PM
Joined: Jan 2013
Posts: 52
Greece
A
Member
Offline
Member
A
Joined: Jan 2013
Posts: 52
Greece
My suggestion is to never write the query like that (day='01.01.2017').

Always be at the safe side writing the code like this :

select * from my_tab t
where t.day = to_date('2017-12-23','YYYY-MM-DD') or to_date('01.02.2017', 'DD.MM.YYYY')
or whatever format you prefer.

If however I cannot convince you smile , you can run the following
SELECT *
FROM nls_database_parameters a
WHERE a.PARAMETER = 'NLS_DATE_FORMAT';

and then use the format returned at "where t.day = ..."

Re: querying for dates by text
Arky #56869 08/03/17 03:57 PM
Joined: Feb 2016
Posts: 80
Ontario, Canada
Member
Offline
Member
Joined: Feb 2016
Posts: 80
Ontario, Canada
I agree with Arky,

be safe and always use either the default literal DATE 'YYYY-MM-DD' or TO_DATE to use any format you want.

Query the way you write it assume the default date format of the combination of the database and client settings, but can *very very* easily drive you to numerous problems if config change/run from another client with different config. This is a bad habit you must change.

I personally always use the literal for date as soon as I do not need timestamp. It is quick, fast, and safe.

Re: querying for dates by text
Nicolas D. #56880 08/08/17 02:17 PM
Joined: Aug 2017
Posts: 4
D
Member
OP Offline
Member
D
Joined: Aug 2017
Posts: 4
while I understand your suggestion, to never use that syntax, it's just boiler plate code that I now have to write dozens of times per day without any necessity for it and always in the exact same way. Just because my PL/SQL-Developer was upgraded from 11 to 12.

I'm not talking about my productive code, that is handled via jdbc and correct. I'm just talking about the daily work as a developer where I'm manually on the database.

If I switch clients and the query I type there isn't working, then I'll rewrite it. There is no hazard.

The NLS_DATE_FORMAT in the database is 'DD-MON-RR'. I definitely won't be using that.

When I use SQLPlus, I can still use my preferred way of writing. And it's terribly sad to see that SQLPlus is more comfortable than PL/SQL Developer now.

How can I have the same comfort as in SQLPlus in PL/SQL-Developer? Is there any way? And why was PL/SQL-Developer made more uncomfortable with version 12?


Re: querying for dates by text
Dirk_1234 #56902 08/16/17 11:33 AM
Joined: Apr 2013
Posts: 34
E
Member
Offline
Member
E
Joined: Apr 2013
Posts: 34
Just alter the nls settings for your session to any format you are using in your selects and it should work. Either at the start of your scripts or at login.sql .
But writing date=[any_formatted_date_sting] is a bad pratice at all, blaming PL/SQL developer is an option but it wont get you nowhere now.

Re: querying for dates by text
eetele #56966 08/29/17 08:54 AM
Joined: Aug 2017
Posts: 4
D
Member
OP Offline
Member
D
Joined: Aug 2017
Posts: 4
I still don't understand this. In my PL/SQL setting, I configured the date format to 'dd.MM.yyyy'. But I have to write ... = DATE '2017-08-29' in the 'yyyy-MM-dd' format.

When I use a different client other than PL/SQL Developer 12 (like sqlplus or PL/SQL Developer 11, etc.) then I can write dates in the 'dd.MM.yyyy' format, so it can't be an issue about my database setting, it must be the client.

Where can I configure the date format for PL/SQL Developer 12? The configuration under setting -> NLS Options -> Date is not working.

Re: querying for dates by text
Dirk_1234 #56969 08/29/17 09:44 AM
Joined: Aug 1999
Posts: 22,220
Member
Offline
Member
Joined: Aug 1999
Posts: 22,220
The preference applies to the date format as displayed in the user interface, such as the SQL Window result grid, Test Window variables, and so on.

To set a specific date format for your Oracle sessions you can use the NLS_DATE_FORMAT setting. It can be set in the Oracle Registry, as a Windows environment variable, or as a PL/SQL Developer parameter.

The PL/SQL Developer parameter can be set in the Params.ini file in the installation directory. Just add the following line:

NLS_DATE_FORMAT=DD.MM.YYYY


Marco Kalter
Allround Automations
Re: querying for dates by text
Dirk_1234 #56972 08/29/17 04:00 PM
Joined: Sep 2003
Posts: 387
London, UK
Member
Offline
Member
Joined: Sep 2003
Posts: 387
London, UK
[quote=Dirk_1234]I still don't understand this. In my PL/SQL setting, I configured the date format to 'dd.MM.yyyy'. But I have to write ... = DATE '2017-08-29' in the 'yyyy-MM-dd' format.[/quote]
An ANSI date literal takes only one format, 'YYYY-MM-DD', as documented. It will therefore always work as expected, regardless of NLS or any other desktop settings. That is the whole point of it. It's worth getting into the habit of always using this for date literals - anything else will one day do something unexpected.


Moderated by  support 

Link Copied to Clipboard
Powered by UBB.threads™ PHP Forum Software 7.7.4
(Release build 20200307)
Responsive Width:

PHP: 7.1.33 Page Time: 0.026s Queries: 14 (0.009s) Memory: 2.5454 MB (Peak: 3.0444 MB) Data Comp: Off Server Time: 2024-05-18 20:30:49 UTC
Valid HTML 5 and Valid CSS