can't insert a date in my tablle in Oracle sql - sql

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.

Related

syntax error when trying to input date and time

I just started with SQL and I'm having a problem when trying to insert an date and time.
The table structure:
CREATE TABLE Voo_Pac
(
codReserva INT NOT NULL PRIMARY KEY,
DataCont DATE,
HoraCont TIME
);
Code I'm trying to use to insert date and time:
INSERT INTO Voo_Pac (codReserva, DataCont, HoraCont)
VALUES (1), (15-08-2019), (12:13:52);
When I try to execute the code, it gives me the following message:
Error 1: could not prepare statement (1 near ":13": syntax error)
I assume you are using MySQL/MariaDB/SQL Server because of the TIME datatype?
Your insert should be
INSERT INTO Voo_Pac (codReserva, DataCont, HoraCont)
VALUES (1, '2019-08-15', '12:13:52');
see demo
You at least need quotes. And depending on your DB maybe a CAST to the apropiated type
INSERT INTO Voo_Pac (codReserva, DataCont, HoraCont)
VALUES 1, '15-08-2019', '12:13:52';

ORA-01858 Error in an Oracle SQL Table

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.

How to specify input format in SQL create table?

I'm new in SQL and I need to create table with specified field format. How to add CHECK condition that will assure that input will be formatted e.g.
[LLLDD]
where L is a letter and D is a digit?
Try this if you are adding the constraint on a new table
CONSTRAINT ck_data_checker CHECK ([columnName] LIKE ('[A-Z][A-Z][A-Z][0-9][0-9]'))
Try this if you are adding the constraint on existing table
ALTER TABLE tableName
ADD CONSTRAINT ck_data_checker CHECK ([columnName] LIKE ('[A-Z][A-Z][A-Z][0-9][0-9]'))
Try this: http://sqlfiddle.com/#!6/3974b
create table test (
field1 char(5),
check (field1 like '[a-z][a-z][a-z][0-9][0-9]')
);
insert into test values ('ttt09'); --this will succeed
If you were to change the insert to:
insert into test values ('testi'); -- this will fail
insert into test values ('12345'); -- this will fail
I'm no sql server expert, but I think you can add a LIKE with a regular expression. Have a look at these websites
https://msdn.microsoft.com/en-us/library/ms179859.aspx
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/dc127433-2982-4065-b290-f411a075a694/use-regular-expressions-to-check-sql-server-2012-table-fields?forum=databasedesign

error while adding a column in oracle 10 g

i am trying to add a column validFrom to my table
this is my query
alter table mytable add validFrom date default getdate() not null
i am getting error like
ORA-04044: procedure, function, package, or type is not allowed here
please help
Oracle does not have a getdate() function - something which can easily be found out by looking into the manual.
You need to use SYSDATE or CURRENT_DATE
ALTER TABLE mytable ADD (validFrom date DEFAULT sysdate);

Insert empty string into INT column for SQL Server

A SAMPLE table has only one column ID of type int, default null.
In Oracle when I do:
insert into SAMPLE (ID) values ('');
the new record is added with blank value. But in SQL Server 2008, when I run the same insert statement, the new record has the value of 0.
Is there a way to force SQL Server 2008 to default blank string to NULL instead of 0 (for numerical type of columns)?
Assuming that your INSERT statement is part of a stored procedure re-used in many places of your application (or, perhaps, is a batch always constructed by the same part of the client code) and that the inserted value is a number passed as a string argument, you could modify the INSERT like this:
INSERT INTO SAMPLE (ID) VALUES (NULLIF(#argument, ''));
Use NULL instead.
insert into SAMPLE (ID) values (NULL);
How about another idea - define an INSTEAD OF INSERT Trigger.
Despite the fact that you're trying to insert a string, with this the operation is "intercepted", empty string is replaced by NULL, and the insert succeeds.
If you define this trigger on your table, then you can continue to insert empty string as before, with no other changes.
Edit: As Martin Smith points out, this effectively is a comparison to 0 (the equivalent of empty string as an int) meaning you won't be able to store 0 in this table. I leave this answer here in case that's acceptable to your situation - either that or re-do all your queries!
CREATE TRIGGER EmptyStringTrigger
ON [SAMPLE]
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO [SAMPLE](ID)
SELECT CASE
WHEN ID = '' THEN NULL
ELSE ID
END
FROM inserted
END
SQL Fiddle example
You can't insert a 'string' into a int column. Oracle must be just handling that for you.
Just try inserting NULL if that's what you need.
insert into SAMPLE (ID) values (NULL);
One more option
insert into SAMPLE (ID) values (DEFAULT)