SQL Server datetime out of range issue - sql

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.

Related

In Oracle SQL developer how do you insert date and time

I am Unable to insert date data and time data into oracle table. Please find the below query and error
INSERT INTO Sunday_Service (
Service_ID,
Service_date,
Start_time,
End_time,
Service_location,
No_of_children)
Values (
Seq_service_id.nextVal,
TO_DATE('YYYY-MM-DD', '2022-07-03'),
TO_DATE('hh24:mi:ss', '09:00:00'),
TO_DATE('hh24:mi:ss', '10:15:00'),
'RM 2101',
'10');
error:
Error report ORA-01821
You have your parameters switched in your TO_DATE function calls. Your TO_DATE function calls should look like this:
TO_DATE ('2022-07-03', 'YYYY-MM-DD'),
TO_DATE ('09:00:00', 'hh24:mi:ss'),
TO_DATE ('10:15:00', 'hh24:mi:ss')
You have the arguments to to_date() the wrong way around:
INSERT INTO Sunday_Service (Service_ID, Service_date,
Start_time, End_time,
Service_location, No_of_children)
VALUES (Seq_service_id.nextVal,
TO_DATE('2022-07-03', 'YYYY-MM-DD'),
TO_DATE('09:00:00', 'hh24:mi:ss'),
TO_DATE('10:15:00', 'hh24:mi:ss'),
'RM 2101',
'10');
But you probably want to combine the time and date, rather than holding them in separate columns; so if you removed service_date from your table you could do:
INSERT INTO Sunday_Service (Service_ID,
Start_time, End_time,
Service_location, No_of_children)
VALUES (Seq_service_id.nextVal,
TO_DATE('2022-07-03 09:00:00', 'YYYY-MM-DD HH24:MI:SS'),
TO_DATE('2022-07-03 10:15:00', 'YYYY-MM-DD HH24:MI:SS'),
'RM 2101',
'10');
Apart from anything else, that will make it possible to handle service calls that span midnight or multiple days.
You could also use timestamp literals:
...
VALUES (Seq_service_id.nextVal,
TIMESTAMP '2022-07-03 09:00:00',
TIMESTAMP '2022-07-03 10:15:00',
...
or slightly more explcitly:
...
VALUES (Seq_service_id.nextVal,
CAST(TIMESTAMP '2022-07-03 09:00:00' AS DATE),
CAST(TIMESTAMP '2022-07-03 10:15:00' AS DATE),
...
If no_of_children is a number column, as it appears, then the last value should be a number - 10 rather than '10'.

How can I substract today date (or find interval id days) in a table?

I am using Vertica SQL and have the following table:
CREATE temp TABLE XXX (DATE TEXT, PRICE INTEGER);
INSERT INTO XXX VALUES
('2019-04-27 01:00', 1), ('2019-04-27 02:30', 3), ('2019-04-27 18:00',2),
('2019-04-28 17:00', 2), ('2019-04-28 21:00', 5),
('2019-04-29 17:00',50), ('2019-04-29 21:00',10),
('2019-04-30 17:00',10), ('2019-04-30 21:00',20),
('2019-05-01 17:00',40), ('2019-05-01 21:00',10),
('2019-05-02 17:00',10), ('2019-05-02 21:00', 6);
I want to find a difference (compute in days) between today and dates in the table.
I tried to use
select (date_trunc('month',DATE)-date_trunc('month',current_date)) as DATE, PRICE from XXX
As I understood initial table has 13 rows, however date_trunc('month',current_date) has 1 row. As a result I have an error. How this issue can be solved?
And as I use Vertica should I add PostgreSQL or MySQL in tags?
Try with either datediff or timestampdiff
datediff('day', date , Now()) as DaysBetweenDates
timestampdiff(day, date, Now()) as DaysBetweenDates

Insert Statement error, ORA-00984: column not allowed here

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.

Insertion in database of type timestamp and data containing apostrophe using ORACLE-11g

I have creater the table successfully as follows:
CREATE TABLE TOY_STORE
(
TOY_STORE_ID NUMBER(3) PRIMARY KEY,
TOY_STORE_NAME VARCHAR2(30) NOT NULL,
CITY VARCHAR2(30) DEFAULT 'Delhi',
PHONENUMBER NUMBER(10) NOT NULL UNIQUE,
STORE_OPENING_TIME TIMESTAMP,
STORE_CLOSING_TIME TIMESTAMP
);
ALTER TABLE TOY_STORE ADD CHECK (EXTRACT(HOUR FROM CAST (TO_CHAR (STORE_OPENING_TIME, 'YYYY-MON-DD HH24:MI:SS') AS TIMESTAMP)) > 8 || NULL);
ALTER TABLE TOY_STORE ADD CHECK (EXTRACT(HOUR FROM CAST(TO_CHAR(STORE_CLOSING_TIME, 'YYYY-MON-DD HH24:MI:SS') AS TIMESTAMP)) < 22 || NULL);
Now I want to enter data in the table. I executed the following command (here the second data is "Kid's Cave"),
INSERT INTO TOY_STORE VALUES(1, 'Kid''s Cave', 'Delhi', 9912312312, 2014-04-01 09:10:12, 2014-04-01 21:42:05);
But it showed the following error..
ORA-00917: missing comma
Please explain
You need to put the dates inside ''. Try this:
INSERT INTO TOY_STORE
VALUES(1, 'Kid''s Cave', 'Delhi', 9912312312, '2014-04-01 09:10:12', '2014-04-01 21:42:05');
On a side note:
I will suggest you to use varchar() to store PhoneNumbers instead of Number datatype
Dates don't have a single quote in your query - Use like so:
'2014-04-01 09:10:12', '2014-04-01 21:42:05'
The problem isn't the ', it's the fact that you are not quoting the date literals. Oracle interprets it as several integer literals with operators between them, and fails because there's no comma separating them.
Surrounding them by quotes (') should work:
INSERT INTO TOY_STORE VALUES
(1,
'Kid''s Cave',
'Delhi',
9912312312,
'2014-04-01 09:10:12',
'2014-04-01 21:42:05');
But it's a bad practice, as it assumes the format matches the database's default date format, which makes your code error prone and unprortable. A better approach would be to explicitly convert these values to timestamps with an explicitly stated format:
INSERT INTO TOY_STORE VALUES
(1,
'Kid''s Cave',
'Delhi',
9912312312,
TO_TIMESTAMP('2014-04-01 09:10:12', 'YYYY-MM-DD HH24:MI:SS'),
TO_TIMESTAMP('2014-04-01 21:42:05', 'YYYY-MM-DD HH24:MI:SS'));
Try using to_date to convert string into date
INSERT INTO TOY_STORE
VALUES (1, 'Kid''s Cave', 'Delhi', 9912312312,
to_date('2014-04-01 09:10:12', 'yyyy-mm-dd hh24:mi:ss'),
to_date('2014-04-01 21:42:05', 'yyyy-mm-dd hh24:mi:ss'));
you can also use TIMESTAMP literal
INSERT INTO TOY_STORE
VALUES (1, 'Kid''s Cave', 'Delhi', 9912312312,
TIMESTAMP '2014-04-01 09:10:12',
TIMESTAMP '2014-04-01 21:42:05');

sum before joining two table

CREATE TABLE Daily
([DATE] datetime, [sales] int)
;
INSERT INTO Daily
([DATE], [sales])
VALUES
('2012-01-01 00:00:00', 1),
('2012-01-02 00:00:00', 2),
('2012-01-03 00:00:00', 3),
('2012-01-04 00:00:00', 4),
('2012-01-05 00:00:00', 5),
('2012-01-06 00:00:00', 6),
('2012-01-06 00:00:00', 5),
('2012-01-07 00:00:00', 7),
('2012-01-08 00:00:00', 8),
('2012-01-09 00:00:00', 9),
('2012-01-10 00:00:00', 10),
('2012-01-11 00:00:00', 11),
('2012-01-12 00:00:00', 12),
('2012-01-13 00:00:00', 13),
('2012-01-14 00:00:00', 14),
('2012-01-15 00:00:00', 15),
('2012-01-16 00:00:00', 16)
;
CREATE TABLE Weekly
([Weekly] datetime)
;
INSERT INTO Weekly
([Weekly])
VALUES
('2012-01-07 00:00:00'),
('2012-01-14 00:00:00'),
('2012-01-21 00:00:00')
;
i want the final output
Sales
1/7/2012 33
1/14/2012 77
any help on this would be appreciated. thanks in advance
I would strongly reccommend against storing this in a table, if any of your daily data changes your weekly data will need to be changed to or it will be wrong, instead create a view as follows:
CREATE VIEW Weekly
AS
SELECT WeekEnd = DATEADD(WEEK, DATEDIFF(WEEK, 0, [DATE]) + 1, -2),
Sales = SUM(Sales)
FROM Daily
GROUP BY DATEADD(WEEK, DATEDIFF(WEEK, 0, [DATE]) + 1, -2);
You can use this in the same way you would the table you want create, but this will always be in sync with the daily data. If you want to change your week start/end day (i.e. monday-sunday) you can change the -2 in the DATEADD function to alter this.
Example on SQL Fiddle
(Based on the [] around column names I am guessing this is SQL-Server.)