We use Oracle 10.2.0.4.0 database, oracle form builder and report builder for creating forms and reports.
Now the problem is in our production database nls_date_format is dd-mon-rr format. When developer create form in developer suit they give dd-mm-rr format at form level and when data stored in table that date format is dd-mm-rr.
Now when developer run form or report within form builder it gives dd-mm-rr format.but when same form or report run from application server side it gives junk characters in month.date and year print same as date format only month display in junk characters.
Hope you all guide well.
There are two issues.
when data stored in table that date format is dd-mm-rr.
This is completely wrong. Oracle doesn't store the date in the format you see, what you see is for display. Oracle stores DATE in an internal proprietary format in 7 bytes with each byte representing different elements of the DATE.
Byte Description
---- -------------------------------------------------
1 Century value but before storing it add 100 to it
2 Year and 100 is added to it before storing
3 Month
4 Day of the month
5 Hours but add 1 before storing it
6 Minutes but add 1 before storing it
7 Seconds but add 1 before storing it
Do not depend on the locale_specific NLS_DATE_FORMAT. Always use:
TO_CHAR to display the date in your desired format
TO_DATE to explicitly convert the string into date.
Remember, TO_DATE is NLS dependent.
If you only have a date element, and if you do not care about the time element, then better use ANSI Date literal which follows a fixed format 'YYYY-MM-DD'.
only month display in junk characters
This is again because you are depending on the NLS_DATE_LANGUAGE. As I said, you should avoid depending on the locale-specific client settings. Explicitly mention the NLS_DATE_LANGUAGE or use ANSI Date literal if you are not concerned about the time element.
For example,
SQL> SELECT TO_CHAR(SYSDATE, 'DD-MON-RR') dt FROM DUAL;
DT
---------
26-OCT-15
SQL> alter session set nls_date_language='french';
Session altered.
SQL> SELECT TO_CHAR(SYSDATE, 'DD-MON-RR') dt FROM DUAL;
DT
-----------
26-OCT. -15
So, what happened above? for a person using FRENCH nls_date_language, the MONTH is showing junk value. Let's make it NLS independent by explicitly mentioning the nls_date_language.
SQL> SELECT TO_CHAR(SYSDATE, 'DD-MON-RR', 'nls_date_language=english') dt FROM DUAL;
DT
---------
26-OCT-15
Also, the NLS_LANG value might not be correctly set in the OS environmental variable. See Why are junk values/special characters/question marks displayed on my client?
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;
Hi I am pretty much new to Oracle and need an idea on how to do it.
Consideer I have a Text file and the value in the file is
'20180924'
'20180923'
I read these value from the Pro*c and then I add 29 days to it, so the value now changes from 20180924 to 20180953. I don't want that to happen i want the value to be added as
TO_DATE('20180924','yyyymmdd')+29 which returns the value as "23-OCT-18"
The answer is correct but i want the result to also be in the same YYYYMMDD format like 20181023.
How do i achieve this ?
Just format it back.
to_char(TO_DATE('20180924','yyyymmdd')+29, 'yyyymmdd')
Dates do not have a format - they are represented internally by 7 or 8 bytes.
If you want a date to have a format then you will need to convert it to a data type that can be formatted - i.e. a string:
TO_CHAR( TO_DATE( '20180924', 'yyyymmdd' ) + 29, 'YYYYMMDD' )
When SQL/Plus (or SQL Developer) displays date data types they implicitly convert them to strings (since this is more meaningful to you, the user, than displaying the raw bytes) and uses the NLS_DATE_FORMAT session parameter as the format model in this implicit conversion. If you want to change this then you can use:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYYMMDD';
and then your query:
TO_DATE( '20180924', 'yyyymmdd' ) + 29
should (provided your SQL client uses this setting and does not have their own internal format preferences) give the output you expect. (Note: this will change the default format for the client to display all dates in this session and not just this one statement.)
select TO_NUMBER((TO_CHAR(TO_DATE(HIREDATE,'DD-MON-YY'),'YYYYMMDD')) FROM EMP;
select TO_NUMBER((TO_CHAR(TO_DATE(HIREDATE,'DD-MON-YY'),'YYYYMMDD'))FROM EMP
20201101
select TO_NUMBER((TO_CHAR(TO_DATE(SYSDATE,'DD-MON-YY'),'YYYYMMDD')) FROM DUAL;
Want to split DB2 date having format 2018-04-12-14.02.16.058110 to Oracle in two different columns with format YYYYMMDD and other with HHMM.
You can't do that. Oracle DATE datatype contains both DATE and TIME components, so - although you might try with different TO_CHAR (or, worse, TO_DATE or TO_TIMESTAMP) functions you find on the Internet, the final result will be the same: values will have both date & time.
Therefore, I'd suggest you to do exactly that: store YYYYMMDD HHMI (note MI - minutes, not MM - month).
For example:
SQL> select
2 cast(to_timestamp('2018-04-12-14.02.16.058110',
3 'yyyy-mm-dd-hh24.mi.ss,ff6') as date) result
4 from dual;
RESULT
-------------------
12.04.2018 14:02:16
SQL>
I have created a table in Oracle in which I have KPI_START_DATE column which is a Date datatype, and KPI_START_TIME which is a TIMESTAMP datatype.
Now I want to modify this date dataype for
KPI_START_DATE to dd/mm/yyyy
and
KPI_START_TIME to HH:MI:SS.
So that user should always enter the date and time in this column in this proper format.
I tried below query but its was giving error:
Alter table KPI_DEFINITION MODIFY(to_char(KPI_START_DATE,'dd/mm/yyyy') )
DATE and TIMESTAMP columns do not have any inherent readable format. The values are stored in Oracle's own internal representation, which has no resemblance to a human-readable date or time. At the point to retrieve or display a value you can convert it to whatever format you want, with to_char().
Both DATE and TIMESTAMP have date and time components (to second precision with DATE, and with fractional seconds with TIMESTAMP; plus time zone information with the extended data types), and you should not try to store them separately as two columns. Have a single column and extract the information you need at any time; to get the information out of a single column but split into two fields you could do:
select to_char(KPI_START, 'dd/mm/yyyy') as KPI_START_DATE,
to_char(KPI_START, 'hh24:mi:ss') as KPI_START_TIME
but you'd generally want both together anyway:
select to_char(KPI_START, 'dd/mm/yyyy hh24:mi:ss')
Also notice the 'hh24' format model to get the 24-hour clock time; otherwise you wouldn't see any difference between 3 a.m. and 3 p.m.
You can store a value in either type of column with the time set to midnight, but it does still have a time component - it is just midnight. You can't store a value in either type of column with just a time component - it has to have a date too. You could make that a nominal date and just ignore it, but I've never seen a valid reason to do that - you're wasting storage in two columns, and making searching for and comparing values much harder. Oracle even provides a default date if you don't specify one (first day of current month). But the value always has both a date and a time part:
create table KPI_DEFINITION (KPI_START date);
insert into KPI_DEFINITION (KPI_START)
values (to_date('27/01/2015', 'DD/MM/YYYY'));
insert into KPI_DEFINITION (KPI_START)
values (to_date('12:41:57', 'HH24:MI:SS'));
select to_char(KPI_START, 'YYYY-MM-DD HH24:MI:SS') from KPI_DEFINITION;
TO_CHAR(KPI_START,'YYYY-MM-DDHH24:MI:SS')
-----------------------------------------
2015-01-27 00:00:00
2015-01-01 12:41:57
Your users should be inserting a single value with both date and time as one:
insert into KPI_DEFINITION (KPI_START)
values (to_date('27/01/2015 12:41:57', 'DD/MM/YYYY HH24:MI:SS'));
select to_char(KPI_START, 'YYYY-MM-DD HH24:MI:SS') from KPI_DEFINITION;
TO_CHAR(KPI_START,'YYYY-MM-DDHH24:MI:SS')
-----------------------------------------
2015-01-27 12:41:57
You can also use date or timestamp literals, and if using to_date() you should always specify the full format - don't rely on NLS settings as they may be different for other users.
You should understand difference between datatype and format. DATE is a datatype. TIMESTAMP is a datatype. None of them have formats, they're just numbers.
When converting character datatype to or from date datatype, format should be applied. It's an attribute of an actual conversion, nothing else.
Look at this:
SQL> create table tmp$date(d date);
Table created
SQL> insert into tmp$date values (DATE '2010-11-01');
1 row inserted
SQL> insert into tmp$date values (DATE '2014-12-28');
1 row inserted
SQL> select d, dump(d) from tmp$date;
D DUMP(D)
----------- ---------------------------------
01.11.2010 Typ=12 Len=7: 120,110,11,1,1,1,1
28.12.2014 Typ=12 Len=7: 120,114,12,28,1,1,1
There is no any 'format' here.
DISPLAYING and STORING are NOT the same when it comes to DATE.
When people say Oracle isn’t storing the date in the format they wanted, what is really happening is Oracle is not presenting the date in the character string format they expected or wanted.
When a data element of type DATE is selected, it must be converted from its internal, binary format, to a string of characters for human consumption. The conversion of data from one type to another is known as known a “conversion”, “type casting” or “coercion”. In Oracle the conversion between dates and character strings is controlled by the NLS_DATE_FORMAT model. The NLS_DATE_FORMAT can be set in any of several different locations, each with its own scope of influence.
I could go on with my leacture over DATE data type, but I am glad that someone has already got a good writeup over this. Please read this https://edstevensdba.wordpress.com/2011/04/07/nls_date_format/
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