Why cant i insert a date sql table - sql

Trying to insert dates into my SQL Server table, I am using the format YYYYMMDD
I'm getting this error
Msg 241, Level 16, State 1, Line 201
Conversion failed when converting date and/or time from character string
This is the table I created:
CREATE TABLE SupplierOrders
(
supplierOrderID CHAR(5) NOT NULL,
orderDate DATE NOT NULL,
orderTotal NUMERIC(20,1) NOT NULL,
status VARCHAR(25) NOT NULL,
orderReceiveDate DATE,
orderPaymentDate DATE ,
paymentRefNo DATE ,
quotationID CHAR(6) NOT NULL UNIQUE,
PRIMARY KEY (supplierOrderID),
foreign key (quotationID)
references QuotationProduct(quotationID)
on update cascade on delete no action
);
and this is the data I'm trying to insert
INSERT INTO SupplierOrders
VALUES('s9021', '20150101', 10, 'delivered', '20150101', '20150101', 'po900', 'qo1021');
What am I doing wrong?

The problem is this column in your table.
paymentRefNo DATE ,
I think maybe you meant to make it a char or varchar.
You are trying to insert 'po900' into it.

Related

How to seperate Date and Time in Oracle SQL

I have a table called transaction that contains a TxDateTime which is a DATE data type. When I run a query I would like to separate the date and time into their own columns; TxDate and TxTime. How can I achieve this??
Here is how I created the table and inserted the date/time:
Table creation
CREATE TABLE TRANSACTION(TxNbr INTEGER PRIMARY KEY,
TxCode CHAR(1) NOT NULL,
AccountNbr INTEGER NOT NULL,
Amount DECIMAL(13,2) NOT NULL,
TxDateTime DATE,
RefNbr VARCHAR(3),
FOREIGN KEY(AccountNbr) REFERENCES ACCOUNT (AccountNbr) ON DELETE SET NULL,
FOREIGN KEY(TxCode) REFERENCES TX_TYPE (TxCode) ON DELETE SET NULL
);
Insert into table
INSERT INTO TRANSACTION VALUES(TxNbr_Seq.nextval, 'X', 1000001, 123.45, TO_DATE('2019/05/01 12:00', 'yyyy/mm/dd hh24:mi'), '101');
Select
SELECT Transaction.TxDateTime,
FROM TRANSACTION
The output only gives the date: 19-05-01, but I would like the date and time in separate columns when I run a query.
Oracle doesn't have a time data type. You can convert to a string:
select to_char(TxDateTime, 'YYYY-MM-DD') as date, to_char(TxDateTime, 'HH24:MI:SS')
from transaction t;

sql conversion of a varchar data type to a datetime data type out-of-range

I am trying to add to a table a group of values on of them is a date.
When trying to add a date i receive the following error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
i have tried to run the following query's:
INSERT INTO BoxEntries (Date,Value,Description,Empid,EmpName) Values(CAST('27/07/2017 10:24:13' AS DATETIME),'0','Alpha Day','0','Alpha')
INSERT INTO BoxEntries (Date,Value,Description,Empid,EmpName) Values(CONVERT(VARCHAR,'27/07/2017 10:24:13',13),'0','Alpha Day','0','Alpha')
INSERT INTO BoxEntries (Date,Value,Description,Empid,EmpName) Values(CONVERT(VARCHAR,'27-07-2017 10:24:13.000',113),'0','Alpha Day','0','Alpha')
INSERT INTO BoxEntries (Date,Value,Description,Empid,EmpName) Values('27-07-2017 10:24:13.000','0','Alpha Day','0','Alpha')
I have confirmed and 13 or 113 is the time of datatime i want in SQL.
The wired part is that when i try to directly add to the database the values it doesn't give me any errors.
The table:
CREATE TABLE [dbo].[BoxEntries] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Date] DATETIME NOT NULL,
[Value] MONEY NOT NULL,
[Description] VARCHAR (MAX) NOT NULL,
[EmpId] INT NOT NULL,
[EmpName] VARCHAR (MAX) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC) );
mssql format of datetime is 'YYYY-MM-DD HH:MM:SS.mmm'
https://learn.microsoft.com/en-us/sql/t-sql/data-types/datetime-transact-sql
so correct query for your case might be:
INSERT INTO BoxEntries ([Date],Value,Description,Empid,EmpName)
Values('2017-07-27 10:24:13.000', '0', 'Alpha Day', '0', 'Alpha');

How to insert varchar (date) to DateTime field in SQL table

I need to write an insert query to insert Date(string) to a DateTime field.
This is my table
CREATE TABLE [dbo].[TSTKSInterfaceRun](
ID INT IDENTITY(1,1),
AttendanceDate [datetime] NULL,
DateOfInterfaceRun [datetime] NULL,
TotalRecords [decimal](4,2) NULL,
Company [varchar](200) NULL
) ON [PRIMARY]
I have tried using this query
Insert into [TSTKSInterfaceRun] (AttendanceDate,DateOfInterfaceRun,TotalRecords,Company) VALUES(CONVERT(Datetime, '2017-05-01 18:01:00', 120),CONVERT(Datetime, '2017-05-01 23:00:00', 120),1500,'SANCO')
but not working.
Error show as
Arithmetic overflow error converting int to data type numeric.
in SSMS.
I need a query to insert a string(date) to DateTime column in SQL table
Thanks in advance
Your value 1500 is too large for a scale (4) and the precision(2). With (4,2) the max digits is 4 with 2 being decimal places. The maximum number that this field can hold is 99.99

"Not a valid month" or number

Getting this error while trying to put a few inserts into a table.
Getting an error regarding not a valid month and when I try change it around i'm getting invalid number error.
ORA-01843: not a valid month ORA-06512: at "SYS.DBMS_SQL"
Code:
CREATE TABLE ExpenseReport (
ERNo NUMERIC(10) NOT NULL,
ERDesc VARCHAR(255) NOT NULL,
ERSubmitDate DATE DEFAULT CURRENT_TIMESTAMP,
ERStatusDate DATE NOT NULL,
ERStatus VARCHAR(8) DEFAULT 'PENDING',
SubmitUserNo NUMERIC(10) NOT NULL,
ApprUserNo NUMERIC(10) NOT NULL,
CONSTRAINT ExpenseReport_CK1 CHECK (ERStatusDate >= ERSubmitDate),
CONSTRAINT ExpenseReport_CK2 CHECK (ERStatus = 'PENDING'/'APPROVED'/'DENIED'),
CONSTRAINT ExpenseReport_PK1 PRIMARY KEY(ERNo),
CONSTRAINT ExpenseReport_FK1 FOREIGN KEY(SubmitUserNo) REFERENCES Users(UserNo),
CONSTRAINT ExpenseReport_FK2 FOREIGN KEY(ApprUserNo) REFERENCES (USerNo)
);
INSERT INTO ExpenseReport
(ERNo, ERDesc, ERSubmitDate, ERStatusDate, ERStatus, SubmitUserNo, ApprUSerNo)
VALUES (1,'Sales Presentation','8/10/2002','8/26/2002','APPROVED',3,4);
I've also tried using the TO_DATE but having no luck there,
by any chance can anyone see where i'm going wrong.
Use the DATE keyword and standard date formats:
INSERT INTO ExpenseReport (ERNo, ERDesc, ERSubmitDate, ERStatusDate, ERStatus, SubmitUserNo, ApprUSerNo)
VALUES (1, 'Sales Presentation', DATE '2001-08-10', DATE '2001-08-2006', 'APPROVED', 3, 4);
In addition to the satisfaction of using standard date formats, this protects you against changes in local settings.
In your DDL statement:
CONSTRAINT ExpenseReport_CK2 CHECK (ERStatus = 'PENDING'/'APPROVED'/'DENIED')
Should be:
CONSTRAINT ExpenseReport_CK2 CHECK (ERStatus IN ( 'PENDING', 'APPROVED', 'DENIED' ) )
When you are trying to insert values the check constraint is being evaluated and it is trying to perform a division operation on the three string values'PENDING'/'APPROVED'/'DENIED' which results in ORA-01722: invalid number.
Once you change this then using TO_DATE('01/01/02','DD/MM/YY') (as you wrote in comments) or an ANSI date literal DATE '2002-01-01' should work in your DML statements.
(Note: Be careful using 2-digit years or you can find that dates are inserted with the wrong century.)
Check your date format: select sysdate from dual;
and enter as it show. OR
change your date format: alter session set nls_date_format= 'DD-Mon-YYYY HH24:MI:SS';
It Was Easy :
if Your code Like This just remove hem and write that
Example :
Your code : values ('30178','K111', '22/12/2008')
Do This : values ('30178','K111', '22/Dec/2008')

SQL Server 2012 - Auto Increment and Null / Not Null

I have created the following table for apartments and used auto-increment on column ApartmentID. I thought once I set this auto-increment to 101, it would automatically increase in the rows as I insert, however I just get a null value.
My Create table code is;
CREATE TABLE Apartments
(
ApartmentID smallint,--AUTOINCREMENT = 101,
Occupier smallint NULL default 0,
Rent money default 0,
CurrentOccupier smallint NULL,
)
Because I have auto increment included in my create table, I insert values as follows;
INSERT INTO dbo.Apartments (Occupier, Rent, CurrentOccupier)
VALUES
('1','400','21'),
('0','450','90'),
The following is my result;
ApartmentID Occupier Rent CurrentOccupier
NULL 1 400.00 21
NULL 0 450.00 90
Concerned that the ApartmentID column was showing NULL instead of 101, 102, 103 etc and thinking this was to do with the column property null, I dropped the table an recreated it at follows;
CREATE TABLE Apartments
(
ApartmentID smallint NOT NULL,--AUTOINCREMENT = 101,
Occupier smallint NULL default 0,
Rent money default 0,
CurrentOccupier smallint NULL,
)
The result / message I got was
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'ApartmentID', table 'Apartment_2.dbo.Apartments'; column does not
allow nulls. INSERT fails.
I would like to work with the first table I created, if I could get the values 101, 102 etc to show up in the column ApartmentID, when I run the query select * from Apartments instead of the word null.
Any / advice help would be appreciated. Again it may be a simple error, but being new I do not recognize my error. Also I have included -- infront of auto increment, as I have seen that online.
Thanks
Josie
In SQL Server you declare the column as IDENTITY not autoincrement. The syntax to declare such a column seeded at 101 is
CREATE TABLE Apartments
(
ApartmentID smallint NOT NULL IDENTITY(101,1) PRIMARY KEY,
Occupier smallint NOT NULL default 0,
Rent money NOT NULL default 0,
CurrentOccupier smallint NULL
)
I assumed that likely you will want this column to be the primary key. Remove those keywords if this is not the case.
Please try the below code. This should help you.
CREATE TABLE Apartments ( ApartmentID smallint IDENTITY(101,1) NOT NULL, Occupier smallint NULL default 0, Rent money default 0, CurrentOccupier smallint NULL);