Home » SQL & PL/SQL » SQL & PL/SQL » date conversion
date conversion [message #10140] Tue, 06 January 2004 04:42 Go to next message
ranjith
Messages: 10
Registered: November 2000
Junior Member
how can cahnge the date format to mm-dd-yyyy from default format(mm-dd-rr) to
cos i have to store date in that format
ranjith
Re: date conversion [message #10141 is a reply to message #10140] Tue, 06 January 2004 05:07 Go to previous messageGo to next message
Rishi Maini
Messages: 4
Registered: January 2004
Junior Member
Use command:-

sql> alter session set nls_date_format="mm-dd-yyyy";

I think it will work..actually I am also a newbie to Oracle...but still try it...

Regards
Rishi
Re: date conversion [message #10142 is a reply to message #10140] Tue, 06 January 2004 05:15 Go to previous messageGo to next message
Thiru
Messages: 1089
Registered: May 2002
Senior Member
Hi,
The date column is stored in the database in Oracle's proprietry format irrespective of the format you use for 'displaying'.
SQL> select value from V$nls_parameters where parameter='NLS_DATE_FORMAT';

VALUE
----------------------------------------------------------------
DD-MON-RR

SQL> select sysdate from dual;

SYSDATE
---------
06-JAN-04

SQL> alter session set nls_date_format='mm-dd-yyyy';

Session altered.

SQL> select sysdate from dual;

SYSDATE
----------
01-06-2004

SQL> alter session set nls_date_format='DD/MM/YY HH:MI';

Session altered.

SQL> select sysdate from dual;

SYSDATE
--------------
06/01/04 10:18


-Thiru
Re: date conversion [message #10143 is a reply to message #10142] Tue, 06 January 2004 05:22 Go to previous messageGo to next message
Thiru
Messages: 1089
Registered: May 2002
Senior Member
you use to_char to specify the date format for display.

SQL> select to_char(sysdate,'MM-DD-YYYY') from dual;

TO_CHAR(SY
----------
01-06-2004
Re: date conversion [message #10144 is a reply to message #10143] Tue, 06 January 2004 05:26 Go to previous messageGo to next message
Rishi Maini
Messages: 4
Registered: January 2004
Junior Member
Will he be able to insert values in Date Column using Char Column Value...???? Or One has to again change the Char value to Date Format...
Re: date conversion [message #10146 is a reply to message #10144] Tue, 06 January 2004 06:41 Go to previous messageGo to next message
Thiru
Messages: 1089
Registered: May 2002
Senior Member
You can insert a character string , containing the date , into a date column , provided the string matches(or compatible) your session date format. Or else you can and preferably use to_Date to interpret your character string.


SQL> create table t(dt date);

Table created.

SQL> insert into t values('10-DEC-99');

1 row created.

-- 10-DEC-99 character string is compatible with my session date format DD-MON-RR

-- using to_date to interpret the string 28/SEP/03

SQL> insert into t values(to_date('28/SEP/03','DD/MON/YY'));

1 row created.

SQL> insert into t values('050505');
insert into t values('050505')
                     *
ERROR at line 1:
ORA-01861: literal does not match format string

-- 050505 fails becos its incompatible with DD-MON-RR

SQL> insert into t values(to_date('050505','DDMMYY'));

1 row created.

-- so used to_date to inform Oracle of the date format

-- dates are displayed in the same session date format,irrespective of the format you used for inserting

SQL> select * from t;

DT
---------
10-DEC-99
28-SEP-03
05-MAY-05

-- you can change the display format to anything you want

SQL> select to_char(dt,'YYYY-MM-DD') from t;

TO_CHAR(DT
----------
1999-12-10
2003-09-28
2005-05-05

Re: date conversion [message #10160 is a reply to message #10146] Tue, 06 January 2004 22:48 Go to previous messageGo to next message
ranjith
Messages: 10
Registered: November 2000
Junior Member
i already set this one but the error still there and also used to_char

SQL> alter session set nls_date_format='mm/dd/yyyy'
and changed in init file also
SQL> create table PRJCT
2 (
3 PRJCT_ID NUMBER(10) not null,
4 PRJCT_NAME VARCHAR2(50) not null,
5 PRJCT_DESC VARCHAR2(2000) null ,
6 STRT_DATE DATE null
7 constraint CKC_STRT_DATE_PRJCT check (
8 STRT_DATE is null or (STRT_DATE between '01/01/1970' and '31/1
/2050'
9 )),
10 END_DATE DATE null
11 constraint CKC_END_DATE_PRJCT check (
12 END_DATE is null or (END_DATE between '01/01/1970' and '31/12/
050'
13 )),
14 PRDCTN VARCHAR2(1) null
15 constraint CKC_PRDCTN_PRJCT check (
16 PRDCTN is null or (PRDCTN in ('Y','N'))),
17 PRJCT_OWNR_ID NUMBER(5) not null,
18 STREAM VARCHAR2(25) not null,
19 constraint PK_PRJCT primary key (PRJCT_ID)
20 using index
21 tablespace PSRS_IDX
22 )
23 tablespace PSRS_TABS
24 /
STRT_DATE is null or (STRT_DATE between '01/01/1970' and '31/12/205
'
*
ERROR at line 8:
ORA-02436: date or system variable wrongly specified in CHECK constraint

pls hlp
ranjith
Re: date conversion [message #10163 is a reply to message #10160] Tue, 06 January 2004 23:30 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
You need to use explicit TO_DATE conversions, including the format, in your check constraints, regardless of your nls_date_format:

CREATE TABLE prjct
  (prjct_id      NUMBER   (  10) NOT NULL,
   prjct_name    VARCHAR2 (  50) NOT NULL,
   prjct_desc    VARCHAR2 (2000) NULL ,
   strt_date     DATE            NULL
   CONSTRAINT    ckc_strt_date_prjct 
   CHECK         (strt_date IS NULL 
                  OR (strt_date BETWEEN <b>TO_DATE ('01/01/1970', 'DD/MM/YYYY')</b> 
                                AND     <b>TO_DATE ('31/1/2050', 'DD/MM/YYYY')</b>)),
   end_date      DATE NULL
   CONSTRAINT    ckc_end_date_prjct 
   CHECK         (end_date is NULL 
                  OR (end_date BETWEEN <b>TO_DATE ('01/01/1970', 'DD/MM/YYYY')</b> 
                               AND     <b>TO_DATE ('31/12/2050', 'DD/MM/YYYY')</b>)),
   prdctn        VARCHAR2 (   1) NULL
   CONSTRAINT    ckc_prdctn_prjct 
   CHECK         (prdctn is NULL 
                  OR (prdctn IN ('Y','N'))),
   prjct_ownr_id NUMBER   (   5) NOT NULL,
   stream        VARCHAR2 (  25) NOT NULL,
   CONSTRAINT    pk_prjct 
   PRIMARY KEY   (PRJCT_ID)
   USING INDEX TABLESPACE prs_idx)
  TABLESPACE psrs_tabs
/
Re: date conversion [message #10177 is a reply to message #10160] Wed, 07 January 2004 05:11 Go to previous messageGo to next message
Thiru
Messages: 1089
Registered: May 2002
Senior Member
Ranjith,
as Barbara mentioned, you will need to use TO_DATE when creating check constraints using dates. Generally,to_date is good idea in any usage so as not to rely on the default or nls_date_format.

About this error message :

2436, 00000, "date or system variable wrongly specified in CHECK constraint"
// *Cause: An attempt was made to use a date constant or system variable,
// such as USER, in a check constraint that was not completely
// specified in a CREATE TABLE or ALTER TABLE statement. For
// example, a date was specified without the century.
// *Action: Completely specify the date constant or system variable.
// Setting the event 10149 allows constraints like "a1 > '10-MAY-96'",
// which a bug permitted to be created before version 8.

-Thiru
thanks ,tnsalias name [message #10191 is a reply to message #10177] Wed, 07 January 2004 23:07 Go to previous messageGo to next message
renjith
Messages: 5
Registered: November 2001
Junior Member
thanks
i used to_char with the date fields in table creation time
and its worked
just one more dought
how can we connect with forms with oracle server i am using forms 9i .i used connect string how can we define tns alias name
with regards
renjith
thanks
Re: thanks ,tnsalias name [message #10206 is a reply to message #10191] Thu, 08 January 2004 11:37 Go to previous messageGo to next message
Thiru
Messages: 1089
Registered: May 2002
Senior Member
you will have to define your TNSNAMES connect string in $ORACLE_HOME/network/admin/tnsnames.ora file of your Forms ORACLE_HOME. You can either manually edit this file or use Net Configuration assistant to specify the Host,Port,Service_name(or SID),Protocol for the tns alias you are defining.

-Thiru
Re: date conversion [message #10427 is a reply to message #10146] Wed, 21 January 2004 15:07 Go to previous messageGo to next message
Nancy Campbell
Messages: 2
Registered: January 2004
Junior Member
Trying to run this query:

select entered_by, entered_date, changed_date, to_date(ltrim(substr(labour_audit_id, length(labour_audit_id) - 13, 6)), 'mm-ddd-yy'), record_note
from employees_notes
where entered_by = 'user'

My error is:

ORA-01861: literal does not match format string
Re: date conversion [message #10430 is a reply to message #10427] Wed, 21 January 2004 20:44 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
Your error message is saying that

(substr(labour_audit_id, length(labour_audit_id) - 13, 6))

which is only six characters long, is not in the format

mm-ddd-yy

which is 9 characters long. It is also an unusual date format, with three d's instead of the usual two.

You need to fix things so that both are in the same format, perhaps changing mm-ddd-yy to mmddyy, if the six character string is correct.
Re: date conversion [message #10777 is a reply to message #10163] Tue, 17 February 2004 02:06 Go to previous messageGo to next message
Vinaya
Messages: 1
Registered: February 2004
Junior Member
Hi,

Instead of using "ALTER SESSION SET NLS_DATE_FORMAT = 'XXXXXXXX'" is there any other way to set the parameter permenently?

Vinaya
Re: date conversion [message #10806 is a reply to message #10777] Wed, 18 February 2004 03:42 Go to previous message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
You can set it in your init.ora file, your login.sql file or glogin sql file, and you can set it in an after logon trigger. Please click on the link below for additional information from Tom Kyte.

Previous Topic: variable conditional increament
Next Topic: Help required to understand the following Syntax table(cast,cast(multiset,The(select(cast
Goto Forum:
  


Current Time: Thu Apr 25 03:20:15 CDT 2024