data type in Oracle - sql

how to resolve this?
INSERT INTO logiciel VALUES ('log1','Oracle 6',13-05-1995,'6.2','UNIX','3000');
INSERT INTO logiciel VALUES ('log1','Oracle 6',13-05-1995,'6.2','UNIX','3000')
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected DATE got NUMBER
the error
here's the table
the table
create table logiciel (
nLog varchar2 (5) primary key,
nomLog varchar (20) not null,
dateAchat date,
version varchar2 (7),
typeLog varchar2 (9),
prix number (6,2)
);

Use date literal or TO_DATE function with appropriate format mask. Do not insert strings into date datatype columns, hoping that Oracle will guess format you used.
SQL> -- date literal is always DATE 'yyyy-mm-dd'
SQL> INSERT INTO logiciel (nlog, nomlog, dateachat, version, typelog, prix)
2 VALUES ('log1', 'Oracle 6', date '1995-05-13', '6.2', 'UNIX', '3000');
1 row created.
SQL> -- TO_DATE lets you choose format, but then the format mask must follow it
SQL> INSERT INTO logiciel (nlog, nomlog, dateachat, version, typelog, prix)
2 VALUES ('log2', 'Oracle 6', to_date('13-05-1995', 'dd-mm-yyyy'), '6.2', 'UNIX', '3000');
1 row created.
SQL>

You have to use quotes around your date value as dates are treated as string -
INSERT INTO logiciel VALUES ('log1','Oracle 6','13-05-1995','6.2','UNIX','3000');
But only using string will tell the DB to store it as string not as date. The difference between them is, You can not do any date manipulation on strings. So you have 2 option now.
Use To_Date function with date format -
INSERT INTO logiciel VALUES ('log1','Oracle 6',TO_DATE('13-05-1995', 'DD-MM-YYYY'),'6.2','UNIX','3000');
Use DATE keyword which is supported by ANSI standard but with that, you have to use the date format as 'YYYY-MM-DD'-
INSERT INTO logiciel VALUES ('log1','Oracle 6',DATE '1995-05-13','6.2','UNIX','3000');

Related

Creating a default column using sysdate but getting a character varying error when inserting data

I am creating a table in redshift using a default sysdate column However when I insert data I get a weird expression is of type character varying error. This is the first time I ever ran into this error.
CREATE TABLE IF NOT EXISTS shipments_swp
(
shipment_id bigint DISTKEY ENCODE RAW,
user_id bigint ENCODE ZSTD,
last_tracking_event_time timestamp encode zstd,
bd_updated_at timestamp default sysdate ENCODE ZSTD
);
Insert into shipments_swp
(
select *
from common.shipments_dim
);
The error message is as shown
SQL Error [500310] [42804]: [Amazon](500310) Invalid operation: column "bd_updated_at" is of type timestamp without time zone but expression is of type character varying;
Can you post an example of the timestamp you're trying to insert into that column? It's possible that you need to cast that column as a timestamp before you write it to shipments_swp. I tried a simple test to see that the insert works correctly and I had no problem inserting to it:
CREATE TABLE IF NOT EXISTS test_table_a (
ts_a TIMESTAMP DEFAULT SYSDATE ENCODE ZSTD
);
-- Insert a few different date formats
insert into test_table_a values('2020/05/22');
insert into test_table_a values('2020-05-22');
insert into test_table_a values('2020-05-22 16:12:27.830728');
insert into test_table_a values('2020-05-22 16:06:00');
--- Create a second table and write to that
--- to mimic the insert behavior from the question
CREATE TABLE IF NOT EXISTS test_table_b (
ts_b TIMESTAMP DEFAULT SYSDATE ENCODE ZSTD
);
INSERT INTO test_table_b (
SELECT *
FROM test_table_a
);
--- This insert succeeds
SELECT * FROM test_table_b;
2020-05-22 16:06:00
2020-05-22 00:00:00
2020-05-22 16:06:56.823844
2020-05-22 00:00:00

ORA-01481: invalid number format model in oracle insert query

I have below query where i am getting error as
ORA-01481: invalid number format model
The user is attempting to either convert a number to a string via TO_CHAR or a string
to a number via TO_NUMBER and has supplied an invalid number format model parameter.
I am using this query in stored procedure. The day_id column is number data type and the value stored in this column as YYYYMMDD. The month_id column is also number data type and i want to stored the value in this column as YYYYMM
INSERT INTO TEST_CHECK(MONTH_ID) VALUES
(to_char(REC.day_id, 'YYYYMM'));
You're applying a conversion to a number as if it was a date, trying to use the result (a string) as a number.
SQL> create table TEST_CHECK(MONTH_ID number);
Table created.
SQL> INSERT INTO TEST_CHECK(MONTH_ID) VALUES(to_char(20180101, 'YYYYMM'));
INSERT INTO TEST_CHECK(MONTH_ID) VALUES(to_char(20180101, 'YYYYMM'))
*
ERROR at line 1:
ORA-01481: invalid number format model
You may need something like:
SQL> INSERT INTO TEST_CHECK(MONTH_ID) VALUES(substr(to_char(20180101), 1, 6));
1 row created.
I would remember that storing dates and months in such a way is not a good idea.
Use
INSERT INTO TEST_CHECK(MONTH_ID) VALUES
(substr(REC.day_id, 1, 6));
instead, since both day_id and month_id are numbers.
If day_id were in date format you could make such a conversion to char but this not the case.
You may apply to_char conversion for REC.day_id as to_char(REC.day_id) but if there's no non-numeric character, oracle considers number as char implicitly without to_char, during a string operation such as substr.

Missing comma in SQL while insert

Code:
CREATE TABLE PERSONNE1 (
NUMP NUMBER,
PRENOM VARCHAR2(25),
NOM VARCHAR2(25),
DATENAIS DATE,
CONSTRAINT PK_NUMP PRIMARY KEY (NUMP)
);
create sequence numpersonne
start with 0
increment by 2
minvalue -1;
SQL> insert into PERSONNE1 (numpersonne.nextval, "Jack", "Nicholson", to_date('04/22/1937','dd/mm/yyyy'));
insert into PERSONNE1 (numpersonne.nextval, "Jack", "Nicholson", to_date('04/22/1937','dd/mm/yyyy'))
Error:
ERROR at line 1:
ORA-00917: missing comma
String literals in SQL are denoted by single quotes ('), not double quotes (").
More importantly, as noted in the comments, you're missing the values keyword:
insert into PERSONNE1
values -- was missing
(numpersonne.nextval,
'Jack', -- Fix the quotes here
'Nicholson', -- And here
to_date('04/22/1937','mm/dd/yyyy'));
Edit:
The date format was also wrong - you need to switch dd and mm.
The immediate cause of the error you're seeing is that you've got your values in the wrong place; at the moment they're (sort of) part of the column list, and you can't have a function call inside that - so the opening parenthesis is being flagged. And that's because you're missing the values keyword.
As #Mureinik said, the string enclosure is a single quote, not a double quote; and you also have your day and month the wrong way round:
insert into PERSONNE1
values (numpersonne.nextval, 'Jack', 'Nicholson', to_date('04/22/1937','mm/dd/yyyy'));
or using a date literal to avoid that potential for confusion:
insert into PERSONNE1
values (numpersonne.nextval, 'Jack', 'Nicholson', date '1937-04-22');
It is also a good idea to explicitly list the columns you're inserting into:
insert into PERSONNE1 (NUMP, PRENOM, NOM, DATENAIS)
values (numpersonne.nextval, 'Jack', 'Nicholson', date '1937-04-22');

SQL - literal does not match format string in INSERT INTO

I've created an empty table--in my website that holds a bunch of tables-- that has the following columns/data types:
NAME -- VARCHAR2
MRN -- NUMBER
DATE_S -- DATE
E -- DATE
DELI -- DATE
WB -- VARCHAR2
ST_ID -- VARCHAR2
COMMENTS --VARCHAR2
EI -- NUMBER
Below is one of almost 800 rows of code I am using to populate the table.
INSERT INTO SANDBOX.W_C VALUES ('S,E',11300033,'2012-02-18 00:00:00','2012-03-01 00:00:00','2013-02-18 00:00:00','N','006i',NULL,NULL);
When I run that piece of code I get the following message: literal does not match format string. What am I doing wrong?
You need to_Date
INSERT INTO SANDBOX.W_C VALUES ('S,E',11300033,
TO_DATE('2012-02-18', 'yyyy-mm-dd'),
TO_DATE('2012-03-01', 'yyyy-mm-dd'),
TO_DATE('2013-02-18', 'yyyy-mm-dd'),'N','006i',NULL,NULL);
When you provide a date as a string, the database uses it's default settings to try to convert the string. The best way to handle this is the use of to_date, as in scaisEdge's answer.
However, you can also change the default date mask using alter session before you run the insert statements:
ALTER SESSION SET NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss';

Why can't I enter this date into a table using sql?

Structure of the employee table:
Name Null? Type
------------- -------- ------------
EMPLOYEE_ID NOT NULL NUMBER(38)
FIRST_NAME VARCHAR2(50)
LAST_NAME VARCHAR2(50)
EMAIL VARCHAR2(50)
DEPT_ID NUMBER(38)
SALARY NUMBER(38)
COMMISION_PCT FLOAT(126)
HIREDATE DATE
INSERT statement :
insert into employees_table
values (05,'Sophie','Kuchinskey','sophiekuchinskey#yahoo.com',105,5000000,60,'20-sept-1994');
ERROR at line 1:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
Oracle will do an implicit TO_DATE on non-date literals when inserting them into a DATE column using the value of NLS_DATE_FORMAT as the format mask. If the non-date literal matches this format then it will work (and if it doesn't then it won't) - however, if the NLS_DATE_FORMAT is ever changed then it will immediately break (any be a huge pain to debug as the code that was working won't but no-one will have changed the code).
You can find out your current NLS_DATE_FORMAT with the query:
SELECT VALUE
FROM NLS_SESSION_PARAMETERS
WHERE PARAMETER = 'NLS_DATE_FORMAT';
It is better to explicitly use TO_DATE with the correct format mask or to use an ANSI date literal (i.e. DATE '2014-12-01').
insert into employees_table
values(
05,
'Sophie',
'Kuchinskey',
'sophiekuchinskey#yahoo.com',
105,
5000000,
60,
TO_DATE( '20-sep-1994', 'DD-Mon-YYYY' )
);
Or, using locale/language agnostic ANSI format:
insert into employees_table
values(
05,
'Sophie',
'Kuchinskey',
'sophiekuchinskey#yahoo.com',
105,
5000000,
60,
DATE '1994-09-20'
);
Please convert the date into correct date format
insert into employees_table values(05,'Sophie','Kuchinskey','sophiekuchinskey#yahoo.com',105,5000000,60,to_date('20-sep-1994','dd-Mon-YYYY'));
Also you can't give month as sept . it should be sep.