Oracle TO_DATE function - oracle

I always thought that to_date function string and date format should match. Why is the below statement working fine?
select TO_DATE('20151014','yyyy-mm-dd') from dual;
Is oracle now ignoring the special characters before converting string to date?
This is in Oracle 11g.

I found this interesting and asked the Google, which told me that basically when formats don't match Oracle tries it's hardest to make it work:
https://asktom.oracle.com/pls/apex/f?p=100:11:::NO:RP:P11_QUESTION_ID:9522299800346947976
http://www.oracledba.co.uk/tips/dates_8i.htm
You can use the 'FX' format string to make it match exactly:
http://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements004.htm#r18c1-t65
http://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements004.htm#SQLRF00216
You learn something new every day! Thanks for asking this!

Related

Oracle to_date format issue

I have an insert statement, where one of the inserted fields is date. I use to_date function to convert string to date in this way:
to_date('10-MAY-10', 'DD-MON-RR')
It works fine, but I found, that it allows also variants like:
to_date('10?MAY?10', 'DD-MON-RR')
to_date('10+MAY+10', 'DD-MON-RR')
I'm expecting an Oracle error, however it makes an insert. Could you please explain why or give a reference to relevant documentation?
Oracle will test for other formats if it fails to find a match in the string - you can see the rules for what it looks for here in the documentation.
As an aside, years have four digits. Please make sure you specify all four when you provide a date-as-a-string, where possible; it saves the database from having to guess and potentially getting it wrong. I.e. your original example should be:
to_date('10-05-2010', 'DD-MM-YYYY')
If you need to restrict the date-as-a-string to a specific format, you can use the fx format modifier, which is mentioned earlier in the same document I linked to previously.
eg. to_date('10/05/2010', 'dd-mm-yyyy') would match but to_date('10/05/2010', 'fxdd-mm-yyyy') would fail

date format with inconsistant data Oracle SQL

I am have a freetext column which a date is input to.
is there a way i can force the output to display the same?
for example some people are entering '2/12/15' others are entering '02/12/2015'
how i can i get the output to pick up the dates and change them to a consistant format?
no idea where to start. I have tried "To_char" but it says 'invalid number'
Actually I believe you want to_date( ):
select to_date('2/12/15', 'mm/dd/rrrr')
from dual;
The 'rrrr' for year will format the year correctly. See date formats here: https://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements004.htm
Your best approach here may be to use a PL/SQL function that accepts the string and then attempts a series of date conversions on it with different date format pictures ('DD/MM/YY', 'DD/MM/YYYY' etc) until it finds one that does not raise an exception.
This won't work if some people enter DD/MM/YYYY and others enter MM/DD/YYYY, but will appear to in some cases.

using oracle to_date function without a format mask

this may be a dumb question however.. I have these two selects: 1st one returns a result, while 2nd gives me a not a valid month error. I can't seem to figure out why..
select to_date('09-12-14','dd-mm-rr') - to_date('01/january/2007') from dual;
select to_date('09-12-14','dd-mm-rr') - to_date('01-01-2007') from dual;
Tnx in advance
When you don't specify a format mask, Oracle will use the NLS_DATE_FORMAT setting (and some predefined masks) to determine the date time format mask.
Apparently your server settings support the first format, and not the second. Therefore it is better to always specify your format mask.

using ansi sql syntax for formatting Numeric

I am using two different databases for my project,
Oracle and Apache Derby, and am trying as much as possible to use the ANSI SQL syntax supported by both of the databases.
I have a table with a column amount_paid NUMERIC(26,2).
My old code, which was using Oracle db, needed to retrieve value in this format
SELECT LTRIM(TO_CHAR(amount_paid,'9,999,999,999,999.99'))
How can I convert a numeric value to such a string in the format '9,999,999,999,999.99' using ANSI sql syntax?
I think this is the wrong approach. The format mask is for display purposes, so it really ought to be the concern of the presentation layer. All your data access layer should do is merely execute:
select amount_paid
from your_table
where ....
This syntax will obviously work whatever database your app attaches to.
Then put the formatting code in the front-end, where it belongs.
My knowledge is not encylopedic but as far as I know there isn't an ANSI function to do what you want (although I'd be glad to find out I'm wrong :-). CONVERT converts between character sets but does not, as best I can see, do the formatting work you want. CAST converts values between data types but, again, doesn't do formatting.
If Derby doesn't support the Oracle-style TO_CHAR function you may have to roll your own function, let's call it MY_TO_CHAR. In Oracle the implementation might be
FUNCTION MY_TO_CHAR(nValue IN NUMBER,
strOracle_format IN VARCHAR2,
strDerby_format IN VARCHAR2)
RETURN VARCHAR2
IS BEGIN
RETURN TO_CHAR(nValue, strOracle_format);
END MY_TO_CHAR;
In Derby you'd want to define this function in a similar manner, taking the appropriate value and format and invoking Derby's equivalent of TO_CHAR with the Derby formatting string.
EDIT: I agree with #APC - a lot of these issues disappear if you don't require the backend to do what is basically front-end work.
Share and enjoy.

Oracle last_ddl_time format

I have to query all_objects table where last_ddl_time='01 jan 2010' but it refuses the date format...
Any body give me the exact format to query?
As AKF said, you should be using Trunc unless you know the exact time the DDL was modified. Your query you added in the comments is looking for any objects where the DDL changed at 1/1/2010 00:00:00. Try:
SELECT *
FROM all_objects
WHERE trunc(last_ddl_time) = to_date('01-01-2010','dd-mm-yyyy');
I suggest you to use de date literal:
where trunc(last_ddl_time) = date '2010-01-01'
You can use the to_date function to format your date. If you enter a literal string, Oracle will attempt to convert that string using to_date with a default format 'DD-MON-YY', so your date would look like "01-JAN-10". As Oracle will be using this same function, you might want to put it in yourself and enjoy the finer granularity that custom formatting can provide.
It would be good to note that the dates stored in that column most likely have more precise dates, including hours and minutes, etc. Though you will be taking a bit of a performance hit, you might be better served using trunc(last_ddl_time) if you are testing with =.
There is some good info on Dates in Oracle at this link.
SELECT *
FROM all_objects t
WHERE trunc(t.last_ddl_time, 'DD') = to_date('2010-JAN-01', 'YYYY-MON-DD');

Resources