Have some dates in my local Oracle 11g database that are in this format:
01-JUL-85
How do I change it to this format (mm/dd/yyyy) using PL/SQL:
7/01/1985
With thanks...
If the column is a date datatype then it is only your NLS_DATE settings that are causing them to be displayed in the format DD-MON-YYYY.
To check your current NLS_DATE format run the following:
SELECT value
FROM V$NLS_Parameters
WHERE parameter ='NLS_DATE_FORMAT';
Oracle stores all dates in an internal binary format and uses the NLS_DATE format to display them (unless explicitly told to display them differently).
You can either alter your NLS_DATE settings to MM/DD/YYYY or TO_CHAR the date column using:
TO_CHAR(<date_column>, 'MM/DD/YYYY')
to see the format you require.
You can alter the NLS_DATE format for your current session or alter the database parameters to change the default NLS_DATE format for the database itself.
If the column is a VARCHAR2 type then you'll need to convert to a date first and then you can format the output using either of the methods described above.
See: http://ss64.com/ora/syntax-nls.html
and: http://www.dba-oracle.com/t_nls_date_format_sysdate.htm
e.g.
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY') as current_date
FROM dual;
or
ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YYYY';
SELECT sysdate
FROM dual;
In pure PL/SQL
DECLARE
v_date DATE := sysdate;
BEGIN
DBMS_OUTPUT.put_line(TO_CHAR(v_date, 'MM/DD/YYYY'));
END;
Related
I want to get display of future and past dates in mm/dd/yyyy and dd/mm/yyyy format in Oracle SQL 18C using SQL functions, so I want the code for it. I tried code select sysdate from dual and I get the output 21-JAN-23, but I want output of future and past dates like 23/11/2033 and 16/12/2009 in mm/dd/yyyy and dd/mm/yyyy format.
Format date using TO_CHAR() function
SELECT
TO_CHAR( SYSDATE, 'FMMonth DD, YYYY' )
FROM
dual;
The output would be:
August 1, 2017
Creating a Future or Past Date
In Oracle, a DATE is a binary data type that ALWAYS consists of 7 bytes representing century, year-of-century, month, day, hour, minute and second and is NEVER stored in any particular human-readable format.
Therefore, if you want to get a DATE data type in a particular format then it is impossible as dates never have any format when they are stored.
If you want to get a date you can use:
A date literal:
SELECT DATE '2023-12-31' FROM DUAL;
or, the TO_DATE function:
SELECT TO_DATE('31/12/2023', 'MM/DD/YYYY') FROM DUAL;
Displaying Dates in a Client Application
However, if the problem is how to display a date in a particular format then you need to convert the binary DATE value to a string.
Most client applications (SQL*Plus, SQL Developer, TOAD, C#, Java, etc.) will implicitly convert a binary date to something that is human-readable when they display it and will have settings in the application that determine the default format that it applies to dates.
For SQL*Plus and SQL Developer, you can modify the NLS_DATE_FORMAT session parameter to change how that client application displays dates (note: this does not change how Oracle stores the dates internally, only how it is displayed by the client).
For example:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
or:
ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YYYY';
And then the client application will display dates in that format when you use a SELECT statement.
For other client applications you will need to check the documentation for that application.
Explicitly Formatting Dates as Strings
If you want to display a DATE in a particular format independent of any settings in the client application then you will need to convert the date to a string.
Using TO_CHAR:
SELECT TO_CHAR(DATE '2023-12-31', 'MM/DD/YYYY') AS formatted_date FROM DUAL;
Or, if you are generating the date and formatting it (rather than taking an existing date and formatting it) then you could just use a string literal:
SELECT '31/12/2023' AS formatted_date FROM DUAL;
I am using a bootstrap date picker in a C# web application.
The application connects to an Oracle 11g database
The Date Picker returns the date in the format YYYY-MM-DDThh:mm e.g. 2021-12-21T09:46. None of the to_date functions in 11g that I know of can handle this. I've tried the following.
select TO_DATE ('2021-12-21T09:46', 'YYYY-MM-DDThh:mi') from dual; -- ORA-01821: date format not recognized
select TO_TIMESTAMP ('2021-12-21T09:46', 'YYYY-MM-RRTHH24:MI') from dual; -- ORA-01821: date format not recognized
select TO_TIMESTAMP_TZ ('2021-12-21T09:46', 'YYYY-MM-DDTHH:MI:SS') from dual; -- ORA-01821: date format not recognized
SELECT SYS_EXTRACT_UTC(TIMESTAMP '2021-12-21T09:46:00') FROM DUAL; -- ORA-01821: date format not recognized
select TO_UTC_TIMESTAMP_TZ ('2021-12-21T09:46:00', 'YYYY-MM-DDThh:mm:ss') from dual; -- ORA-00904: "TO_UTC_TIMESTAMP_TZ": invalid identifier
Is there another function that I can use?
Is there a way of changing the format that the datepicker returns? Please note that I need to change the RETURNED date not the DISPLAYED date.
The database doesn't like the T in the date format mask. Fortunately we can handle that by wrapping it in double-quotes:
select TO_DATE ('2021-12-21T09:46', 'YYYY-MM-DD"T"HH24:MI') from dual;
When importing columns of type "Timestamp" to an Oracle DB:
Import tbl:
02.09.13 00:00:00
After importing:
Oracle tbl:
02.09.13 08:23:44,000000000
In the Oracle tbl the type is also "Timestamp".
How can I remove the ",000000000"?
The TIMESTAMP datatype includes fractional seconds; there is no way to remove them within the database whilst maintaining the datatype. If you don't want fractional seconds then put the date into a DATE column.
It shouldn't matter whether the fractional seconds are stored or not. When selecting from the database into anything other than another date datatype you should format the date as required by the client displaying it. The normal method of doing this would be by using the function TO_CHAR(); for instance:
select to_char(column_name, 'dd.mm.yy hh24:mi:ss') from table_name
You can also do this at a session level by changing your NLS settings.
Unrelated to your question, but to address the comment on a comma being part of your timestamp; the default date format is determined by NLS_TERRITORY. A comma is a perfectly valid character to have here. Altering the NLS_TERRITORY, for example to France, it will appear as part of the NLS_DATE_FORMAT:
SQL> alter session set nls_territory = 'FRANCE';
Session altered.
SQL> select systimestamp from dual
2 ;
SYSTIMESTAMP
------------------------------------------------------------
18/09/13 13:09:54,418387 +01:00
I am using Oracle10g database in which a table contains a Column with Date DataType. I am using the following query to get the record:
select to_char(START_TIME, 'YYMMDD HH24:MI:SS') from table;
So from above query, the result will be of type VARCHAR. I have tried to_Date() method but resulted in displaying only DATE. Can i convert VARCHAR to DATETIME format? The result should be of type DATETIME. Please help me how to resolve this problem.
an Oracle date contains both date and time so you can consider it a datetime (there is no datatype of datetime in Oracle). how is DISPLAYS when you select it is entirely up to your client. the default display setting is controlled by the NLS_DATE_FORMAT parameter. If you're just using the date in your pl/sql block then just assign it into a date datatype and select into that variable without to_char and it will work just fine and contain whatever time component is present in your table.
to control the display, for example using nls_date_format:
SQL> select a from datetest;
A
---------
19-FEB-13
SQL> alter session set nls_date_format='YYMMDD HH24:MI:SS';
Session altered.
SQL> select a from datetest;
A
---------------
130219 07:59:38
but again, this is only for display.
Oracle's Date type fields contain date/time values, therefore converting it to Datetime does not make any sense (it's already datetime)
Read more about oracle date types here
Yeah the Date datatype will meet your needs but you will have to jump through some hoops every time to get the exact time out of it. Definitely use the Timestamp datatype.
I have an entity class which has a timestamp property.
If I save an entity instance in oracle, the corresponding timestamp column will be written into Chinese format(I am using oracle 10g with Chinese locale).
22-8ζ -11 07.04.03.926000 δΈε is saved
But I want 2011-08-22 19:04:03.926
How can change column definition or database locale setting?
A timestamp in Oracle is a point in time. As such, there is no format stored with the data. When you retrieve data from a timestamp column, it is displayed by default in the format specified by your NLS_TIMESTAMP_FORMAT or NLS_TIMESTAMP_TZ_FORMAT session variable.
You can always use a specific format with to_char:
SQL> SELECT to_char(systimestamp, 'yyyy-mm-dd hh24:mi:ss.ff3') my_ts FROM dual;
MY_TS
-----------------------------
2011-08-22 14:38:48.351
You could also set a new default for your session with:
SQL> ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT='yyyy-mm-dd hh24:mi:ss.ff3';
Session altered
SQL> select systimestamp from dual;
SYSTIMESTAMP
-------------------------------------------------
2011-08-22 14:42:23.776