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

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

Related

How to specify my table to insert data as time in minutes and second into a column?

I have been struggling for several hours now and I have not been able to find the solution to my problem. This is an assignment, but I am stuck in this part.
CREATE TABLE Trip
(
Trip_Id SERIAL,
Origin VARCHAR(50) NOT NULL,
Destination VARCHAR(50) NOT NULL,
Date_Time_Picked TIMESTAMP NOT NULL DEFAULT CURRENT_DATE,
Estimated_Time TIME NOT NULL DEFAULT CURRENT_TIME,
Price DECIMAL NOT NULL,
PRIMARY KEY(Trip_Id)
);
INSERT INTO Trip (Origin, Destination, Estimated_Time, Price )
VALUES ('Hialeah' ,'Miami Beach', 30:00, 40.00);
The insert statement in postgreSQL shows a error because the time format. The column Estimated_Time is supposed to store the time in minutes and seconds, but the compiler shows an error because interprets 30:00 as hours and seconds. How can I handle the input of the user to save 30:00 as 30 minutes and 0 seconds. The Trip table can be modified, obviously, the insert statement requires a conversion or cast from '30:00' to Time type, but I am lost in how to do it. Unfortunately, books do not explain how this is done. I would greatly appreciate any hint or example. Thanks in advance.
as pointed out by a_horse_with_no_name and jarlh,
Estimated_Time is the duration of the trip, so the format should be interval
CREATE TABLE trip (
trip_id SERIAL,
origin VARCHAR(50) NOT NULL,
destination VARCHAR(50) NOT NULL,
date_time_picked TIMESTAMP WITHOUT TIME ZONE DEFAULT 'now'::text::date NOT NULL,
estimated_time INTERVAL,
price NUMERIC NOT NULL,
CONSTRAINT trip_pkey PRIMARY KEY(trip_id)
)
and the insert sould be
INSERT INTO Trip (Origin, Destination, Estimated_Time, Price )
VALUES ('Hialeah' ,'Miami Beach', '00:30:00', 40.00);

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');

SQL Insert - String or binary data would be truncated for INT values

I'm getting the "String or binary data would be truncated" error when trying to insert integers to one of my tables.
I've read several post about the length of the column vs the length of the value one is inserting, but it doesn't seem to be my case once the columns are all int or smallint type and the values are all maximum two digits.
The table structure is the following:
CREATE TABLE [tblvUserLocation] (
[User_Location_ID] [int] IDENTITY (1, 1) NOT NULL ,
[Location_ID] [int] NULL ,
[Line_Type_ID] [int] NULL ,
[User_ID] [int] NULL ,
[Active] [smallint] NULL CONSTRAINT [DF_tblvUserLocation_Active] DEFAULT (1),
[Last_Updated] [smalldatetime] NULL CONSTRAINT [DF_tblvUserLocation_Last_Updated] DEFAULT (getdate()),
[Last_Updated_By] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_tblvUserLocation_Last_Updated_By] DEFAULT (suser_sname())
) ON [PRIMARY]
GO
The insert I'm trying to run is the following:
insert into tblvUserLocation (Location_ID, Line_Type_ID, [User_ID], Active)
values (20, 2, 41, 1)
And the error I'm getting is the following:
Server: Msg 8152, Level 16, State 2, Line 1 String or binary data
would be truncated. The statement has been terminated.
If that makes any difference, I'm using SQL Server 2000.
Please let me know what your thoughts are.
Thanks!
Looks like the problem comes from your [DF_tblvUserLocation_Last_Updated_By] constraint.
It's pulling the current username which is more than likely longer than the length of your [Last_Updated_By] column VARCHAR(10).
Update your DDL to:
[Last_Updated_By] [varchar] (128)

Why cant i insert a date sql table

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.

SQLite allows insert of string data into datetime column

Table:
CREATE TABLE logaction
(
actype varchar(8) not null,
actime DATETIME not null,
devid int not null
);
SQL:
insert into logaction values ('TEST', '2013-08-22', 1);
insert into logaction values ('TEST', '2013-08-22 09:45:30', 1);
insert into logaction values ('TEST', 'xyz', 2); // shouldn't this fail?
The last record does make it into the table regardless of the non-datetime value for the actime column. Why and how can I enforce that only good data go in?
Here is a SQL Fiddle.
Well, there's just no DATETIME type in Sqlite...
SQLite does not have a storage class set aside for storing dates
and/or times. Instead, the built-in Date And Time Functions of SQLite
are capable of storing dates and times as TEXT, REAL, or INTEGER
values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich
on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these
formats and freely convert between formats using the built-in date and
time functions.
see doc
You could have created your table like that, it wouldn't have changed anything.
CREATE TABLE logaction
(
actype varchar(8) not null,
actime NonexistingType not null,
devid int not null
);
Unfortunately, not really in sqlite - it doesn't have a datetime type - see section 1.2 in here
Here's a simple work around:
CREATE TABLE logaction
(
actype varchar(8) not null,
actime DATETIME not null check( DATETIME(actime) is not null ),
devid int not null
);
sqlite> insert into logaction values ('TEST', '2013-08-22', 1);
sqlite> insert into logaction values ('TEST', '2013-08-22 09:45:30', 1);
sqlite> insert into logaction values ('TEST', 'xyz', 2);
Error: constraint failed
sqlite>