简体   繁体   中英

Is there any implicit date format conversion In Oracle?

I retrieved date from one table in a mm/dd/yyyy hh24:mi:ss format. Now I want to store it in another table in a dd-mon-rr format.

Will any implicit conversion take place? If not, please suggest the query!

Queries used:

select DATETOSTRING(r_date, 'mm/dd/yyyy hh24:mi:ss') as r_date from abc;

insert into efg values(r_date);

Note: Current date format of database is dd-mon-rr .

Dates (stored in tables) are represented by 7 bytes - they do not have any format associated with them. If they are formatted as a string then that is the client program which you are using to access the database applying its own formatting to the date (which you can usually set via the preferences in that program).

If the "date" is stored with a format then you are not storing it as a date but storing it as a string (ie VARCHAR2 ) format.

DATETOSTRING is not an Oracle function - TO_CHAR( datevalue, format_model, nlsparams ) is an Oracle function.

I retrieved date from one table in a mm/dd/yyyy hh24:mi:ss format. Now I want to store it in another table in a dd-mon-rr format.

No, if you retrieved a DATE then you got 7-bytes from the database - the user interface (ie SQL/Plus, SQL Developer, Java, etc.) you are using performed an implicit conversion to make those 7-bytes understandable to you, the user, when it displayed it.

SQL/Plus and SQL developer will use the NLS_DATE_FORMAT , NLS_DATE_LANGUAGE and NLS_TERRITORY session parameters as the default format model and NLS parameters to the TO_CHAR function to perform this implicit conversion - but these are per-user settings and should not be relied upon to be consistent across multiple users (especially in an international setting).

If you want to store a DATE then it has NO format - if you then want to display it with a specific format then you will need to convert it from a DATE to a string using TO_CHAR with your desired format model.

So, you can simply use:

To copy the date from one table to another:

INSERT INTO efg (r_date)
  SELECT r_date FROM abc;

To get the date in your desired format:

SELECT TO_CHAR( r_date, 'dd-mon-rr' ) AS r_date
FROM   efg

You don't store date in a specific format, your date is always stored as a number inside the database. The way it is displayed to you depends on your specific client parameters. For exemple, if you do:

alter session set nls_date_format='dd/mm/yyyy hh24:mi:ss';

all your dates will be displayed in this format, that doesn't mean the date will be stored in this format if you insert to a table, the date will still be a number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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