How to Save Date in Yii with Oracle - oracle

How does one save the date in Yii with Oracle?
$trx->DATE_TRX=date('Y-m-d');
CDbCommand failed to execute the SQL statement:
SQLSTATE[HY000]: General error: 1861
OCIStmtExecute: ORA-01861: literal does not match format string
(ext\pdo_oci\oci_statement.c:148).
The SQL statement executed was:
INSERT INTO "TRX" ("IDMEMBER", "DATE_TRX") VALUES (:yp0, :yp1) RETURNING "ID" INTO :RETURN_ID

I would guess that you're supplying a character literal as the date, in a format such as 'yyyy-mm-dd'. If so then you should explicitly convert that to a date as part of the insert statement ...
INSERT INTO "TRX" ("IDMEMBER", "DATE_TRX")
VALUES (:yp0, to_date(:yp1,'yyyy-mm-dd'))
RETURNING "ID" INTO :RETURN_ID

Related

Cannot convert String to date in Oracle 11g

I'm trying to insert Date into table from user input but something doesn't work.
I have the following Query :
INSERT INTO DOCTORS.TREATMENTS
(START_OF_TREATMENT, END_OF_TREATMENT, DOCTORS_ID,PACIENTS_ID, DIAGNOSIS_ID)
VALUES (TO_DATE(&startdate, 'yyyy/mm/dd'), TO_DATE(&enddate, 'yyyy/mm/dd'), 3, 1, 1);
For start date I set :
2000/10/01
And for end date I set :
2000/11/01
It seem ok for me but I've got the following error :
Error report -
ORA-01858: a non-numeric character was found where a numeric was expected
ORA-06512: at "SYS.STANDARD", line 167
ORA-06512: at line 2
01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
Any one can explain to me why this error occurred.
Best regards,
Petar.
The value of the first parameter passed to TO_DATE must be a string. After substitution with the value you've given your code will look like
TO_DATE(2000/10/01, 'yyyy/mm/dd')
which fails as shown.
The solution is to put the parameter usage in single-quotes to make the substituted value a string, as in
TO_DATE('&startdate', 'yyyy/mm/dd')
This way, when &startdate is substituted you'll get
TO_DATE('2000/10/01', 'yyyy/mm/dd')
which will work as expected.
Do the same for &enddate.
Best of luck.

Spring Batch: SQL error ORA-01858: a non-numeric character was found where a numeric was expected

I'm trying to use this SQL in my spring batch reader. The SQL seems to have a problem:
select DISTINCT ces.COR_ENBL_STG_ID
from HBX_BATCH_COR_ENBL_STG ces
Inner JOIN HBX_INDV_NEG_ACTION ina ON ces.INDV_ID=ina.INDV_ID
where ces.DISP_PROCESSED_FLAG='Y'
AND ina.NEG_ACTION_RUN_RSN_CD in('11054','11055','11065')
AND ces.PGM_BGN_DT+90<'#{jobExecutionContext['latest.completed.startTime']}'
The value of latest.completed.start.time is coming as '07-Oct-16 12:38:58.000000109 PM' from a tasklet using jdbctemplate and hence the SQL is throwing the following error : ORA-01858: a non-numeric character was found where a numeric was expected.
Just FYI the column ces.PGM_BGN_DT is a Date Type. I tried TO_DATE function but it didn't help. Any suggestions please?
As I understand latest.completed.startTime - is a string of characters.
And its look like as oracle timestamp datatype.
If my suggestion are true, You should use format to convert string to timestamp
to_timestamp(latest.completed.startTime, 'dd-mon-yy HH12:MI:SS.FF AM');

SQLPLus vs SQLDeveloper behavior

I'm experiencing a different behavior between SQLPlus and SQL Developer.
Example data:
create table test (
INIT_DATE DATE
);
INSERT INTO test(INIT_DATE) values (sysdate);
COMMIT;
Now I run the following query (notice we're doing an unnecessary to_date because INIT_DATE is already a date):
select to_date(INIT_DATE, 'dd/mm/rrrr') from test;
The result is:
SQLPlus => Return 20/09/16
SQLDeveloper => Throw ORA-01861
I found this answer, so in SQLDeveloper I changed NLS>Format Date to 'DD/MM/RR' and now SQLDeveloper return 20/09/16.
But, if in SQLDeveloper I change NLS to 'DD/MM/RR HH24:MI:SS' again, and I change the query mask to 'DD/MM/RR', SQLDeveloper return an error again:
select to_date(INIT_DATE, 'DD/MM/RR') from test;
Can anyone explain this behavior?
Why SQLDeveloper throw an error if the query mask is 'DD/MM/RR' but not when NLS is 'DD/MM/RR'?
Use TO_CHAR instead of TO_DATE. TO_DATE function converts char argument in specific format given by second parameter to date value.
Your statement
select to_date(INIT_DATE, 'DD/MM/RR') from test;
does first implicit conversion to char, because INIT_DATE is a date. This conversion is in nls default format, depending on your machine settings.
You try convert DATE to DATE through TO_DATE function, but TO_DATE function arguments are strings and as result Oracle convert INIT_DATE column to string and then pass this string into TO_DATE function.
If you use implicit conversion 'string to date' or 'date to string' , then Oracle use the default date format. In different environments default date format may be different.
Try to use an explicit conversion and an appropriate format.
For example:
select to_date(to_char(INIT_DATE, 'dd/mm/rrrr'), 'dd/mm/rrrr') from test;

Inserting date text into table returns `not a valid month`

My NLS_DATE_FORMAT is DD/MM/YY
When I enter values like these
Insert into EVENTREQUEST (EVENTNO,DATEHELD,DATEREQ,FACNO,CUSTNO,DATEAUTH,STATUS,ESTCOST,ESTAUDIENCE,BUDNO) values ('E100',to_date('25/10/13'),to_date('06/06/13'),'F100','C100',to_date('08/06/13'),'Approved',5000,80000,'B1000');
The row is inserted but when I use the following Format this error message appears.
Error starting at line 2 in command:
Insert into EVENTREQUEST (EVENTNO,DATEHELD,DATEREQ,FACNO,CUSTNO,DATEAUTH,STATUS,ESTCOST,ESTAUDIENCE,BUDNO)
values ('E101',to_date('26-OCT-13','DD-MON-RR'),to_date('28-JUL-13','DD-MON-RR'),'F100','C100',null,'Pending',5000,80000,'B1000')
Error report:
SQL Error: ORA-01843:not a valid month
01843. 00000 - "not a valid month"
*Cause:
*Action:
Can anybody help?
Exists somewhere else: Oracle insert failure : not a valid month
Maybe if you specify NLS_DATE_LANGUAGE?
Insert into EVENTREQUEST (EVENTNO,DATEHELD,DATEREQ,FACNO,CUSTNO,DATEAUTH,STATUS,ESTCOST,ESTAUDIENCE,BUDNO)
values ('E101',to_date('26-OCT-13','DD-MON-RR', 'NLS_DATE_LANGUAGE = AMERICAN'),to_date('28-JUL-13','DD-MON-RR', 'NLS_DATE_LANGUAGE = AMERICAN'),'F100','C100',null,'Pending',5000,80000,'B1000')

Leave column out of Oracle insert statement

I have an insert statement that I am trying to run against an Oracle database. The insert statement is very simple, and I only want to insert a value into one column. The table itself has more than one column, but I am only interested in filling one of the columns with data. The format of my query is similar to the one below:
insert into myTable (col1) Values (val1)
However, this throws the following error:
ORA-00904: "col1": invalid identifier
I've checked to make sure that the column is named correctly, and my only other thought is that something is wrong with my syntax. There are no constraints on the table such as primary keys. Is it possible to only insert values into certain columns when executing an insert statement in Oracle?
Check that you didn't quote the column name on creation of the table. If you did, the column name is stored as you quoted it. For example:
create table table1 (
id number(2)
)
has a different column name to this
create table table2 (
"id" number(2)
)
Oracle by default stores the (unquoted) column names in uppercase. Quoted are stored as is.
You can use a DESC table_name to see how the columns are stored.
The following
select id from table1
select iD from table1
select ID from table1
fetch the records, while only
select "id" from table2
will fetch records.
select id from table2
will throw the ORA-00904 : "ID" : invalid identifier error.
You may have inadvertently done the quoting during creation while using tools as established below:
https://community.oracle.com/thread/2349926
Btw: Yes it is possible to insert records for only one column, as long as the other columns do not have a NOT NULL constraint on them.
Actually, I think you may be double quoting the column in the insert statement (not on table creation), although your example is misleading. I guess this because of your error, which says "col1" is invalid, not "COL1" is invalid. Consider this simple test:
SQL> create table mytable
(
col1 varchar2(10)
)
Table created.
SQL> -- incorrect column name, without double quotes
SQL> insert into mytable(col2) values ('abc')
insert into mytable(col2) values ('abc')
Error at line 9
ORA-00604: error occurred at recursive SQL level 1
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 11
ORA-00904: "COL2": invalid identifier
SQL> -- incorrect column name, with double quotes
SQL> insert into mytable("col2") values ('abc')
insert into mytable("col2") values ('abc')
Error at line 12
ORA-00604: error occurred at recursive SQL level 1
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 11
ORA-00904: "col2": invalid identifier
SQL> -- correct column name, without double quotes (works)
SQL> insert into mytable(col1) values ('abc')
1 row created.
SQL> -- correct column name, with double quotes (fails)
SQL> insert into mytable("col1") values ('abc')
insert into mytable("col1") values ('abc')
Error at line 18
ORA-00604: error occurred at recursive SQL level 1
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 11
ORA-00904: "col1": invalid identifier
The last failed insert attempt is what I think you may be doing:
insert into mytable("col1") values ...
based on the error message:
ORA-00904: "col1": invalid identifier
So the solution would simply be remove the double quoting around the column name in the insert.
It's most probably a syntax error
Desc myTable;
insert into myTable (col1) Values ('val1')
Ensure col1 is a valid column in the table, and you're simply not trying to say 'select the left-most column.
edit: Is it possible to only insert values into certain columns when executing an insert statement in Oracle?
Yes if you want to only insert into certain columns then simply specify it
e.g.
insert into myTable (col1, col2, col6) Values ('val1', 'val2', 'val3');
This will only work if the column itself doesn't have a NOT NULL constraint - in which case it will not allow a value to be enetered (unless again there's a default value).

Resources