-1

Please explain how to write a create query where i can write a spcific date in a specific format (suppose dd/MM/yy) in oracle. Suppose i need my columns in my table ORDERS to be- order_id, order_date, quantity

1
  • 1
    DATE '2014-09-07' or to_date('2014-09-07', 'yyyy-mm-dd'). Please read the manual. Oh and: DATE columns do NOT have "a format".
    – user330315
    Sep 7, 2014 at 11:23

1 Answer 1

3

From the documentation:

"The database stores dates internally as numbers. Dates are stored in fixed-length fields of 7 bytes each, corresponding to century, year, month, day, hour, minute, and second."

And what that looks like:

SQL> select dump(sysdate) from dual
  2  /

DUMP(SYSDATE)
--------------------------------------------------------------------------------
Typ=13 Len=8: 222,7,9,7,2,48,32,0

SQL> 

Which actually looks like eight bytes but interestingly a date is nine bytes long:

SQL> select lengthb(sysdate) from dual
  2  /

LENGTHB(SYSDATE)
----------------
               9

SQL> 

Anyway, storage is fixed and entirely independent of the displaying of dates.

The default date format is governed by the NLS_DATETIME_FORMAT, which is defaulted by the NLS_TERRITORY setting. This is how Oracle determines Currencym, number, formats, days of the week and so on. Find out more by reading the Globalization Support guide.

If you want a different default format mask for your dates this can be set at the database level. This is a big decision. Fortunately it can also be set at a more granular level:

SQL> select sysdate from dual
  2  /

SYSDATE
---------
07-SEP-14

SQL> alter session set nls_date_format='Month DD YYYY HH12:MI AM'
  2  /

Session altered.

SQL> select sysdate from dual
  2  /

SYSDATE
--------------------------
September 07 2014 03:05 AM

SQL> 

As far as input of dates goes, Oracle expects strings containing dates to have the same format as that specified by the NLS_DATE_FORMAT. If this is not the case then we have to apply a conversion using the TO_DATE() function and supplying the mask of the string:

SQL> select to_date('31/05/14','DD/MM/YY') from dual
  2  /

TO_DATE('31/05/14','DD/MM/
--------------------------
May       31 2014 12:00 AM

SQL> 
3
  • Sir i do not want to use the sysdate. If possible please explain how to insert the date in dd/MM/yyyy.
    – user4016242
    Sep 9, 2014 at 7:01
  • I just used sysdate as a handy way of showing the structure of a date. My last example shows how to pass a string with a date value which is not in the default format.
    – APC
    Sep 9, 2014 at 19:54
  • You are giving me the select query. I was talking about the insert query about how to insert date in any particular format.
    – user4016242
    Sep 9, 2014 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.