I'm trying to insert data into this table, but it doesn't work, and I was changing the format to DDMMYY, DD/MM/YY, YYYY-MM-DD... and nothing change the error.
Code:
GO
create table PeriodoAcademico(
CodPeriodoAcad varchar(50) not null primary key,
Descripcion varchar(50) not null,
FechaInicio datetime not null,
FechaFin datetime not null,
FechaInicioClases datetime not null,
FechaFinClases datetime not null,
FechaLimitePago datetime not null,
FechaLimitePrematricula datetime not null,
FechaLimiteRetiro datetime not null,
FechaLimitePublicacion datetime not null
);
GO
begin tran
insert into PeriodoAcademico
values
('2019-2020-2', 'Enero-Abril 2020', '2020-01-04 06:00:00 AM', '2020-04-12 11:59:99 PM', '2020-01-08 06:00:00 AM', '2020-04-08 11:59:99 PM',
'2020-04-08 11:59:99 PM', '2020-03-08 11:59:99 PM', '2020-03-21 11:59:99 PM', '2020-04-10 11:59:99 PM');
commit
GO
Error:
Msg 242, Level 16, State 3, Line 77
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
Completion time: 2020-03-30T17:01:44.3689635-04:00
There are only 60 seconds in a minute.
So this '2020-04-08 11:59:99 PM' is invalid. This '2020-04-08 11:59:59 PM' is fine. eg
select cast('2020-03-08 11:59:59 PM' as datetime)
outputs
2020-03-08 23:59:59.000
Related
I am trying to convert a timestamp to date in SQLite.
But it give me always Null back, I try many solution I find out, but any solution works for me
Here is my request :
Thats my SQL script, if you want to try:
CREATE TABLE Appointments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
fromDateTime TIMESTAMP NOT NULL,
toDateTime TIMESTAMP NOT NULL,
name VARCHAR(30) NOT NULL,
description VARCHAR(200),
type VARCHAR(50)
);
Insert INTO Appointments ( fromDateTime, toDateTime, name, description ) VALUES
('21/03/2020 15:00:00', '21/03/2020 16:00:00', 'Test', 'Test Description'),
('22/03/2020 15:00:00', '22/03/2020 16:00:00', 'Test 2', 'Test 2 Description'),
('22/03/2020 16:00:00', '22/03/2020 17:00:00', 'Test 2', 'Test 2 Description'),
('22/03/2020 17:00:00', '22/03/2020 18:00:00', 'Test 2', 'Test 2 Description'),
('21/03/2020 00:00:00', '25/03/2020 23:59:59', 'Test', 'Test Description'),
('27/03/2020 08:00:00', '21/03/2020 12:00:00', 'Test', 'Test Description'),
('02/03/2020 08:00:00', '10/03/2020 12:00:00', 'Joelle', 'Test Joelle');
To expand on #forpas comment, SQLite does not have a TIMESTAMP data type, so when you insert values into your fromDateTime and toDateTime column they are converted to one of SQLite's 5 data storage classes: NULL, INTEGER, REAL, TEXT, BLOB. Since there is no error on INSERT, this gives the impression that SQLite has recognised the value as a timestamp, when in fact the value has just been treated as TEXT. Now to use those values in any of SQLite's Date and Time functions they must either be an ISO-8601 compatible string, the word now, or a number (interpreted as either a Julian day number or a Unix timestamp dependent on the context). So, you need to change your times to YYYY-MM-DD hh:mm:ss format i.e.
Insert INTO Appointments ( fromDateTime, toDateTime, name, description ) VALUES
('2020-03-21 15:00:00', '2020-03-21 16:00:00', 'Test', 'Test Description'),
('2020-03-22 15:00:00', '2020-03-22 16:00:00', 'Test 2', 'Test 2 Description'),
('2020-03-22 16:00:00', '2020-03-22 17:00:00', 'Test 2', 'Test 2 Description'),
('2020-03-22 17:00:00', '2020-03-22 18:00:00', 'Test 2', 'Test 2 Description'),
('2020-03-21 00:00:00', '2020-03-25 23:59:59', 'Test', 'Test Description'),
('2020-03-27 08:00:00', '2020-03-21 12:00:00', 'Test', 'Test Description'),
('2020-03-02 08:00:00', '2020-03-10 12:00:00', 'Joelle', 'Test Joelle');
Note that datetime is simply called with the column as a parameter and returns the string in an ISO-8601 format. To get YYYY-MM-DD format you need to use strftime as well. So your query becomes:
SELECT strftime('%d - %m - %Y', fromDateTime) AS y,
strftime('%Y-%m-%d', fromDateTime) AS x
FROM Appointments
And the output:
y x
21 - 03 - 2020 2020-03-21
22 - 03 - 2020 2020-03-22
22 - 03 - 2020 2020-03-22
22 - 03 - 2020 2020-03-22
21 - 03 - 2020 2020-03-21
27 - 03 - 2020 2020-03-27
02 - 03 - 2020 2020-03-02
Demo on dbfiddle
I need to calculate the number of hours an employee had worked from 21-Jan-2019 to 22-Jan-2019 starttime on the 21st is 21:00 and timeout on 22nd is 07:00 it should be 10hrs but its showing 14hrs.
create table babysitter (
babysitterid char(5) not null primary key,
datein date not null,
timein time not null,
dateout date not null,
timeout time not null,
noofhrswrk int,
amtpaid int
);
insert into babysitter values('BS001', '18-Jan-2019', '10:00', '18-Jan-2019', '16:00', '', '')
insert into babysitter values('BS002', '15-Jan-2019', '13:00', '15-Jan-2019', '20:00', '', '')
insert into babysitter values('BS003', '21-Jan-2019', '21:00', '22-Jan-2019', '07:00', '', '')
insert into babysitter values('BS004', '11-Jan-2019', '08:00', '11-Jan-2019', '13:00', '', '')
declare #timein time
declare #timeout time
declare #hoursworked datetime
declare Calculate_No_Hrs cursor for
select timein, timeout, noofhrswrk from babysitter
open Calculate_No_Hrs
fetch next from Calculate_No_Hrs into #timein, #timeout, #hoursworked
while (##FETCH_STATUS = 0)
begin
update babysitter
set noofhrswrk = abs(datediff(hour, timeout, timein))
fetch next from Calculate_No_Hrs into #timein, #timeout, #hoursworked
end
close Calculate_No_Hrs
deallocate Calculate_No_Hrs
You'll need to combine your time and date values and then get the difference:
USE Sandbox;
GO
CREATE TABLE babysitter (ID char(5),
StartDate date,
StartTime time(0),
EndDate date,
EndTime time(0),
BlankCol1 char(1),
BlankCol2 char(1));
insert into babysitter values('BS001', '18-Jan-2019', '10:00', '18-Jan-2019', '16:00', '', '')
insert into babysitter values('BS002', '15-Jan-2019', '13:00', '15-Jan-2019', '20:00', '', '')
insert into babysitter values('BS003', '21-Jan-2019', '21:00', '22-Jan-2019', '07:00', '', '')
insert into babysitter values('BS004', '11-Jan-2019', '08:00', '11-Jan-2019', '13:00', '', '')
GO
SELECT DATEDIFF(HOUR,V.StartTime, V.EndTime)
FROM dbo.babysitter
CROSS APPLY (VALUES (DATEADD(SECOND,DATEDIFF(SECOND,0,StartTime),CONVERT(datetime2(0),StartDate)),
DATEADD(SECOND,DATEDIFF(SECOND,0,EndTime),CONVERT(datetime2(0),EndDate)))) V(StartTime, EndTime)
GO
DROP TABLE dbo.babysitter;
Try this - it is giving correct hours when combining date and time before calculating difference in hours
declare #in datetime, #out datetime
set #in = '21-Jan-2019 21:00'
set #out = '22-Jan-2019 7:00'
select DATEDIFF(hh, #in, #out)
The following query is failing with error. Datatype of all the columns are datetime.
Please help.
INSERT INTO [dbo].[TalendJobAudit] ([FeedFileGenDate], [StartTime], [EndTime], [ElapsedTime])
VALUES ('2000-00-00 10:00:00', '2000-00-00 10:00:00', '2000-00-00 10:00:00', '2000-00-00 10:00:00')
GO
I get this error:
Msg 242, Level 16, State 3, Line 4
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
There is no 0 month or 0 day. Your code works fine with correct values:
INSERT INTO TalendJobAudit (FeedFileGenDate, StartTime, EndTime, ElapsedTime)
VALUES('2000-01-01 10:00:00',
'2000-01-01 10:00:00',
'2000-01-01 10:00:00',
'2000-01-01 10:00:00'
);
Here is a SQL Fiddle.
2000-00-00 is not a valid date, please use correct date in place.
any help guys I got error column not allowed here for datetime !
INSERT INTO MEMBERS_CONTRIBUTIONS (
CONTRIBUTION_TYPE,
FROM_DATE,
TO_DATE,
ADDED_PERIOD_IN_MONTHS,
MEMBER_AMOUNT,
THE_CURRENCY,
MATURITY_DATE
) VALUES (
4,
convert(datetime, '6/1/2016 12:00:00 AM', 5),
convert(datetime, '6/1/2016 12:00:00 AM', 5),
0,
2500,
'OMR',
convert(datetime, '6/30/2016 12:00:00 AM', 5)
);
You are trying to use the SQL Server CONVERT() function in Oracle - the Oracle CONVERT() function converts from one character-set to another and does not do what you want.
Instead, you can use a date literal:
INSERT INTO MEMBERS_CONTRIBUTIONS (
CONTRIBUTION_TYPE,
FROM_DATE,
TO_DATE,
ADDED_PERIOD_IN_MONTHS,
MEMBER_AMOUNT,
THE_CURRENCY,
MATURITY_DATE
) VALUES (
4,
DATE '2016-06-01',
DATE '2016-06-01',
0,
2500,
'OMR',
DATE '2016-06-30'
);
In Oracle, all DATE types have both a date and time component - the date literal syntax will just set the time component to 00:00:00 (or 12:00:00 AM in a 12 hour clock).
Or if you want to specify the time component then you can use the timestamp literal (which Oracle will implicitly cast to a DATE type if that is the type of the column you are storing it in):
INSERT INTO MEMBERS_CONTRIBUTIONS (
CONTRIBUTION_TYPE,
FROM_DATE,
TO_DATE,
ADDED_PERIOD_IN_MONTHS,
MEMBER_AMOUNT,
THE_CURRENCY,
MATURITY_DATE
) VALUES (
4,
TIMESTAMP '2016-06-01 00:00:00',
TIMESTAMP '2016-06-01 00:00:00',
0,
2500,
'OMR',
TIMESTAMP '2016-06-30 00:00:00'
);
Or you could explicitly cast a string literal to a date using the TO_DATE() function:
INSERT INTO MEMBERS_CONTRIBUTIONS (
CONTRIBUTION_TYPE,
FROM_DATE,
TO_DATE,
ADDED_PERIOD_IN_MONTHS,
MEMBER_AMOUNT,
THE_CURRENCY,
MATURITY_DATE
) VALUES (
4,
TO_DATE( '6/1/2016 12:00:00 AM', 'MM/DD/YYYY HH12:MI:SS AM' ),
TO_DATE( '6/1/2016 12:00:00 AM', 'MM/DD/YYYY HH12:MI:SS AM' ),
0,
2500,
'OMR',
TO_DATE( '6/30/2016 12:00:00 AM', 'MM/DD/YYYY HH12:MI:SS AM' )
);
The expression convert(datetime, '6/1/2016 12:00:00 AM', 5) calls for a column with the name datetime. But your insert statement doesn't offer a context involving any columns at all, so the query parser can't make any sense of datetime. Hence your ORA-00948 error.
I guess you're trying to put a date/time constant, which from your example could mean either 1-Jun-2016 or 6-Jan-2016, into a date datatype. You need to use the TO_DATE() function to convert your strings to that format. I'm not going to suggest a particular form of the call, because I don't know exactly how your strings are formatted.
do i need to change the data type of the DATE column to VARCHAR?
SQL> INSERT INTO BW_CLASS VALUES(`PC101', `MS OFFICE BASICS', `INDIANA JONES','18','1000',
2 TO_DATE('01-10-2013 10:30 AM', 'MM-DD-YYYY HH:Mm PM'),
3 TO_DATE('05-10-2013 10:30 AM', 'MM-DD-YYYY HH:Mm PM'),
4 `1276';
SP2-0552: Bind variable "MM" not declared.
SQL> desc bw_class
Name Null? Type
CLASS_ID NOT NULL CHAR(5)
CLASS_NAME NOT NULL VARCHAR2(40)
PROFESSOR NOT NULL VARCHAR2(50)
NUMBER_OF_STUDENTS NUMBER(6,2)
COST NUMBER(6,2)
START_DATE NOT NULL DATE
END_DATE NOT NULL DATE
ROOM_NUM VARCHAR2(3)
please use this query
INSERT INTO BW_CLASS VALUES('PC101', 'MS OFFICE BASICS', 'INDIANA JONES','18','1000',
TO_DATE('01-10-2013 10:30 AM', 'MM-DD-YYYY HH:MI PM'),
TO_DATE('05-10-2013 10:30 AM', 'MM-DD-YYYY HH:MI PM'),
'1276';
You used a wrong quote. All parameters need to be quoted using ' in both sides, but you used ` in some places. So you should try this:
INSERT INTO BW_CLASS VALUES('PC101', 'MS OFFICE BASICS', 'INDIANA JONES','18','1000',
TO_DATE('01-10-2013 10:30 AM', 'MM-DD-YYYY HH:MI PM'),
TO_DATE('05-10-2013 10:30 AM', 'MM-DD-YYYY HH:MI PM'),
'1276';
And you don't need to write 2 3 4 at the beginning of lines, why did you do that?