I've been fighting with this problem for a few days.
I created the next table in Oracle's SQL Developer:
CREATE TABLE EMPLEADOSMM (
ID_EMPLEADO VARCHAR2 (10) PRIMARY KEY,
NOMBREEM VARCHAR2 (15) NOT NULL,
AP_PATEM VARCHAR2 (20) NOT NULL,
AP_MATEM VARCHAR2 (20) NOT NULL,
FECHA_NAC DATE NOT NULL,
FECHA_ING DATE NOT NULL,
ID_CARGO VARCHAR2 (10));
Then, I proceeded to add some values to the table, the thing is that it added some like this:
INSERT INTO EMPLEADOSMM VALUES ('100006','OSCAR','MARIN','PEREZ','12-jul-85','15-nov-17','C0002');
But this others send me the 'ORA-01858: a non-numeric character was found where a numeric was expected' error.
INSERT INTO EMPLEADOSMM VALUES ('100004','FABIAN','RODRIGUEZ','VELEZ','31-aug-87','13-jul-17','C0003');
INSERT INTO EMPLEADOSMM VALUES ('100005','LUZ MARIA','TORINO','YAÑEZ','11-dec-90','13-jul-17','C0003');
I tried changing the year format by "yyyy", rewriting all the zeros, but nothings seems to work. Some ideas?
First, I want to emphasize that the code does work. Here is a SQL Fiddle demonstrating the working code.
Second, that means that something about your system causes it to break. Barbaras Ozhan seems to have the right explanation -- internationalization settings recognize some month abbreviations as being the same as English, but not all of them.
You should be writing the insert as:
INSERT INTO EMPLEADOSMM (ID_EMPLEADO, NOMBREEM, AP_PATEM, AP_MATEM, FECHA_NAC, FECHA_ING, ID_CARGO)
VALUES ('100006', 'OSCAR', 'MARIN', 'PEREZ', DATE '1985-07-12', DATE '2017-11-15', 'C0002');
INSERT INTO EMPLEADOSMM (ID_EMPLEADO, NOMBREEM, AP_PATEM, AP_MATEM, FECHA_NAC, FECHA_ING, ID_CARGO)
VALUES ('100004', 'FABIAN', 'RODRIGUEZ', 'VELEZ', DATE '1987-08-31', DATE '2017-07-13', 'C0003');
INSERT INTO EMPLEADOSMM (ID_EMPLEADO, NOMBREEM, AP_PATEM, AP_MATEM, FECHA_NAC, FECHA_ING, ID_CARGO)
VALUES ('100005', 'LUZ MARIA', 'TORINO', 'YAÑEZ', DATE '1990-12-11', DATE '2017-07-13', 'C0003');
Oracle supports the ANSI standard keyword DATE for introducing date constants in the ISO/ANSI standard format, YYYY-MM-DD. I strongly recommend that you use this format in all your code. Use TIMESTAMP when there is a time component.
Including the column names is a best practice.
I would question why the employee id is a string, if you are only going to include numbers in it.
Related
When I create a table using bellow SQL query:
CREATE TABLE student(studentID INTEGER PRIMARY KEY,studnetName VARCHAR (30) NOT NULL,birthday Date)
Bellow operations(SQL commands) show me an error:
INSERT INTO student VALUES('226745','Ahmed','24-06-1997')
INSERT INTO student VALUES('226745','sara','28-03-2000')
INSERT INTO student VALUES('226745','Ali','12-02-2007')
the exact error is:
( Conversion failed when converting date and/or time from character string).
Can anybody help to explain why it happens?
It's because sql engine can't figure out the date format automatically (probably confused between m-d-y and d-m-y)
so you can specify the dateformat before you insert, ex in sql server:
set dateformat dmy;
and the same situation in other dbms
You don't specify your database. Most databases support strings in the format 'YYYY-MM-DD' (an ISO 8601-compatible format) for date constants:
INSERT INTO student VALUES('226745', 'Ahmed', '1997-06-24');
INSERT INTO student VALUES('226745', 'sara', '2000-03-28');
INSERT INTO student VALUES('226745', 'Ali', '2007-02-12');
In Standard SQL, date constants are supported by using the date keyword:
INSERT INTO student VALUES('226745', 'Ahmed', DATE '1997-06-24');
INSERT INTO student VALUES('226745', 'sara', DATE '2000-03-28');
INSERT INTO student VALUES('226745', 'Ali', DATE '2007-02-12');
I would also advise you to list the columns explicitly when using insert.
when I try to insert my date values in all my other tables it works fine, except of one. Whatever format I try I always get the error, that the inserted value couldn't be converted from an input char to the expected date format.
That's how I insert my values
-- ServiceTicket
INSERT INTO ServiceTicket
VALUES ('90000', '01-5-2019', '50000', '10000', '70000', 200.00, 100.00, 5.00, 350.00) --Error converting into DATE type
And this is the table structure:
CREATE TABLE dbo.ServiceTicket (
ticketIssueNo INT NOT NULL IDENTITY(1,1) PRIMARY KEY, --Identity autoincrements
serviceDate DATE NOT NULL,
vehicleId CHAR(8) NOT NULL,
customerId CHAR(8) NOT NULL,
inspectionId CHAR(8) NOT NULL,
serviceCost DECIMAL(10,4) NOT NULL CHECK(serviceCost BETWEEN 0.0 AND 99999.0) DEFAULT 0.0,
inspectionCost DECIMAL(10,4) NOT NULL CHECK(inspectionCost BETWEEN 0.0 AND 99999.0) DEFAULT 0.0,
repairCost DECIMAL(2,2) NOT NULL CHECK(repairCost BETWEEN 0.0 AND 99999.0) DEFAULT 0.0,
GST DECIMAL(10,4) NOT NULL DEFAULT 0.0,
amountDue DECIMAL(10,4) NOT NULL CHECK(amountDue BETWEEN 0.0 AND 99999.0) DEFAULT 0.0,
FOREIGN KEY(vehicleId) REFERENCES Vehicle(vehicleId)
ON UPDATE NO ACTION,
FOREIGN KEY(inspectionId) REFERENCES VehicleInspection(inspectionId)
ON UPDATE NO ACTION,
FOREIGN KEY(customerId) REFERENCES Customer(customerId)
ON UPDATE NO ACTION
)
GO
I might overlook something.
Always list the columns when writing an insert statement.
Here's an exact equivalnet of your insert statement, written properly, based on the DDL you've published:
INSERT INTO ServiceTicket
(serviceDate, vehicleId , customerId, inspectionId , serviceCost , inspectionCost, repairCost, GST , amountDue) VALUES
('90000' , '01-5-2019' , '50000' , '10000' , '70000' , 200.00 , 100.00 , 5.00 , 350.00)
I've used tabs so that each value would be perfectly aligned with the column it goes into, that helps a lot when you have a long list of columns.
As you can clearly see, the serviceDate gets the value '90000' - while it shoud clearly be '01-5-2019'
Always use ISO8601 format for string representation of date / datetime values.
Any other format is culture dependent, and the worst thing about it is that it depends on the default language of the login - so different logins might have different results if you use a culture-dependent format. The ISO8601 standard provides two alternatives for datetime formats: yyyy-mm-ddThh:mm:ss or yyyymmddThhmmss. If you are inserting only a date only string value into a DateTime data type column / variable, be sure to use only the second (yyyymmdd) format - because yyyy-mm-dd is still culture dependent with DateTime (but not with Date or with DateTime2 - that's one more reason why you should never use DateTime again.
So the proper way of writing the insert statement would be this:
INSERT INTO ServiceTicket
(serviceDate, vehicleId , customerId, inspectionId , serviceCost , inspectionCost, repairCost, GST , amountDue) VALUES
('2019-05-01', '90000' , '50000' , '10000' , '70000' , 200.00 , 100.00 , 5.00 , 350.00)
(That is, assuming 01-5-2019 stands for May 1st. If it stands for January 5th, it should be 2019-01-05).
The date format '01-5-2019' which you are using does not appear to be a default accepted date literal by SQL Server. Try using '20190501':
INSERT INTO ServiceTicket (serviceDate, ... <other columns>)
VALUES
('20190501', ...);
Note that YYYYMMDD is an unambiguous unseparated ISO format, as the documentation discusses.
The format for inserting date type in SQL is YYYY-dd-MM, You need to change it to '2019-05-01'
Change your date format from '01-5-2019' to '2019-05-01'.
Instead passing date as '01-5-2019' pass CAST('01-5-2019' AS DATE).
The default format for the year data type in SQL is YYYY-MM-DD(If you've not changed it before) Info. So as described here, you should convert the input string into a valid date type.
I am trying to bulk insert these two columns from excel into a temp table ##NBP_Table. However, when I do that I get the following error:
'Operand type clash: int is incompatible with date'
Does that mean the date aren't in the format it should be to be inserted into a table?
create table ##NBP_Table
(
Applicable_Date date,
NBP_Value numeric(4,4)
)
insert into ##NBP_Table
values (01/04/2014,1.7107),
(02/04/2014,1.6482),
(03/04/2014,1.686),
(04/04/2014,1.6681)
To get the date insert working, please try this
create table ##NBP_Table
(
Applicable_Date date
NBP_Value numeric(5,4)
)
insert into ##NBP_Table
values ('01/04/2014',1.7107)
The date needs to be in quotation marks
I have also corrected the numeric data type for you
this date in expression is considered as int so it will be performed / operations,
so please use 'before starting date and ' after ending date.
'01-04-2014'
Create table #NBP_Table
(
Applicable_Date date,
NBP_Value numeric(5,4)
)
insert into #NBP_Table
values ('01-04-2014',1.7107),
('02-04-2014',1.6482),
('03-04-2014',1.686),
('04-04-2014',1.6681)
I'm new in the SQL programming, I've created a table where I want to insert a a date like
create table person (
PANr integer not null,
name Varchar(10),
HNr integer not null,
stuff_date date,
constraint P_NR primary key (PANr)
);
insert into Personen values ('4711','Andreas''15','31.10.1958');
the creating of the table works but the insert command gives this error:
SQL Error: ORA-01843: not a valid month
01843. 00000 - "not a valid month"
any Idea what's wrong here ??
thanks in advance
Use TO_DATE('31.10.1958', 'DD.MM.YYYY').
You have another error.
insert into person
values
('4711','Andreas''15','31.10.1958');
First, you are quoting your integer values. That may or may not cause a problem, but it's a bad idea. Second, you need a comma between Andreas and 15.
If you don't want to use the TO_DATE function you can use the following:
ALTER SESSION SET nls_date_format='yyyy-mm-dd';
Now, your insert statement would look like this:
INSERT INTO PERSONEN VALUES('4711','Andreas','15','1958-10-31');
If you want to be more specific you can define the date format like this:
ALTER SESSION SET nls_date_format='yyyy-mm-dd hh24:mi:ss';
P.S. Please, pay attention on #Dan Bracuk answer.
I'm new to Oracle (I've been using MySQL mainly until now) so this might be a dumb question. But I have created this table (names are not english but ignore that, that is not important):
CREATE TABLE Auta (
id_auto NUMBER(5) UNIQUE NOT NULL,
typ CHAR(10),
specifikacia_typu CHAR(15),
SPZ CHAR(8),
farba CHAR(20),
datum_vyroby DATE,
pocet_miest NUMBER(2),
pociatok_km NUMBER(6),
poplatok_denny NUMBER(4),
poplatok_km NUMBER(2));
Then I tried using this INSERT query:
INSERT INTO Auta VALUES (
1
,'Audi'
,'A6'
,'KE1-1548'
,'cierna'
,'20-12-2004'
,5
,158749
,1356
,88
);
And I get an error:
ORA-01843: not a valid month
The date format I am using is DD-MM-YYYY. I also tried DD.MM.YYYY, DD/MM/YYYY, I also tried switching month and day like this - MM-DD-YYYY, MM/DD/YYYY - and still the same error.
What to do?
Replace:
,'20-12-2004'
...with:
, TO_DATE('20-12-2004', 'DD-MM-YYYY')
Reference: TO_DATE
oracle date format is DD-MON-YY .
Use date format of 20-april-2004 instead of 20-4-2004.
Use DD-MMM-YYYY format while inserting date into the database.
For example, 15-Mar-2013.