How can I correctly set the current date in this specific format into this insert query on an Oracle DB? [closed]

送分小仙女□ 提交于 2019-12-11 19:24:09

问题


I am not so into database and I have to implement an insert this insert query on an Oracle db:

insert into CODA_RX (PK_CODA, FK_TIPO_DOC, FK_PIVA_DESTINATARIO, DATA_IN, FK_STATO, DATA_OUT, CANALE, SIZE_XML, FK_PIVA_MITTENTE) 
       values(70030, 5, 06655971007, '06-MAG-15', 2, '06-MAG-15', 'WEB', 553, 01392380547); 

As you can see there are 2 DATE fields named DATA_IN and DATA_OUT. In the previous version of my query the date value is explicitly written (it is fixed: '06-MAG-15').

How can I use the current date in this specific format '06-MAG-15' (DD-MM-YY)?

Tnx


回答1:


'06-MAG-15' is NOT a DATE, it is a STRING.

  • Use TO_DATE with proper format model to convert a string into date.

  • Also, it is better to avoid NLS dependency.

  • Another thing, avoid 2 digit values for YEAR. Don't reinvent the Y2K bug. Or, use the RR format. But, there are some rules you must know. I prefer YYYY.

For example,

SQL> SELECT TO_DATE('06-MAY-2015', 'DD-MON-YYYY', 'NLS_DATE_LANGUAGE=ENGLISH') FROM DUAL;

TO_DATE('
---------
06-MAY-15

SQL>

How can I correctly set the current date in this specific format into this insert query on an Oracle DB?

This is completely different from what you have explained in the question.

Remember, DATE doesn't have any format. What you see is for display so that we could interpret it easily. Oracle stores DATE in a proprietary format internally in 7 bytes.

If you want to insert current date, then there is no need of any literal and format. Use SYSDATE.

For example,

SQL> alter session set nls_date_format='MM/DD/YYYY HH24:MI:SS';

Session altered.

SQL> SELECT SYSDATE FROM DUAL;

SYSDATE
-------------------
05/21/2015 15:15:05

SQL>



回答2:


You would need to use TO_DATE() in order to convert a string into a DATE datatype. eg:

TO_DATE('06-MAY-2015', 'dd-MON-yyyy', 'nls_date_language=english')

See the documenation for more information on TO_DATE(), and a list of format masks can be found here

Conversely, if you want to display something that's a DATE datatype in a particular format, you can use TO_CHAR() with the appropriate format mask.



来源:https://stackoverflow.com/questions/30369585/how-can-i-correctly-set-the-current-date-in-this-specific-format-into-this-inser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!