Error message in Informix - sql

This is the code issued and the error message:.
dbuser#test:/var/lib/dbspace/bosarc/Active_Sites/Port_Hope> dbaccess labor32<<?
> INSERT INTO SCH_DAILY (ssn,time_start,week_day,time_end,dept_key,pos_id,sched_time,break_minutes,comments,start_time,end_time,report_date,week_start_date) values (000287752,2016-02-04 16:00:00,5,2016-02-04 12:00:00,D000000007,CASHIE,8.0,0,0,16:00 a,12:00 p,2016-02-04 00:00:00,2016-01-29 00:00:00,NULL,NULL,NULL,NULL);
> ?
Database selected.
201: A syntax error has occurred.
Error in line 1
Near character position 178
Database closed.
dbuser#test:/var/lib/dbspace/bosarc/Active_Sites/Port_Hope>
I even tried downloading the file directly from the database and match it to the info I am trying to load.
Database file
555005875|2016-01-21 16:00:00|5|2016-01-21 22:00:00|D000000007|CASHIE|6.0|0||04:00 p |10:00 p|2016-01-21 00:00:00|2016-01-15 00:00:00||||
What I'm trying to insert:
000287752,2016-02-04 16:00:00,5,2016-02-04 12:00:00,D000000007,CASHIE,8.0,0,0,16:00 a,12:00 p,2016-02-04 00:00:00,2016-01-29 00:00:00,NULL,NULL,NULL,NULL

The DATETIME values such as 2016-02-04 16:00:00 must be enclosed in quotes (either '2016-02-04 16:00:00' or "2016-02-04 16:00:00"), or dressed up more ornately — verbosely — as a DATETIME literal: DATETIME(2016-02-04 16:00:00) YEAR TO SECOND. On the whole, the quotes are simpler. This is not necessary when the data is in a load-format file and loaded via the LOAD command of DB-Access, or one of the loader utilities.
The AM/PM time values such as 16:00 a and 12:00 p are more problematic still — unless they're inserted into a character column. Informix supports DATETIME HOUR TO MINUTE which would accept values 00:00 .. 23:59 as valid times within a day.
16:00 a is not normally how you write 4:00 p; you either use a value in the range 01:00 .. 12:59 with the suffix (remembering that all of the times 12:00 a .. 12:59 a occur before 01:00 a), or you use 24-hour clock 00:00 .. 23:59. On the whole, the latter is more sensible. If they're going to be handled with the AM/PM suffixes, then you have to wrap the value up as a string in an appropriate TO_DATE function call that converts the value to DATETIME HOUR TO MINUTE. But the appropriate function is going to reject 16:00 a as invalid.

First, has #gordon-linoff said, you have to enclose the strings and datetime/literals with quotes.
Then, probably you will get the next error:
236: Number of columns in INSERT does not match number of VALUES.
This is because you put 13 attributes on the INSERT and pass 17 VALUES, the NULLS should be the issue.
Then, probably, you can get:
1263: A field in a datetime or interval values is incorrect or an illegal operation specified on datetime field.
If you get this try passing the values of date/time with the TO_DATE function:
TO_DATE('216-02-02 12:16:24','%Y-%m-%d %H:%M:%S')
TO_DATE('23:24','%H:%M') -- 24H format
TO_DATE('11:24 PM','%I:%M %p') -- 12H format AM/PM
Although I believe that the start_time and the end_ time aren’t a date type.
In your case try the next INSERT:
INSERT INTO SCH_DAILY(
ssn,
time_start,
week_day,
time_end,
dept_key,
pos_id,
sched_time,
break_minutes,
comme‌​nts,
start_time,
end_time,
report_date,
week_start_date
) VALUES (
'000287752',
'2016-02-04 16:00:00',
5,
'2016-02-04 12:00:00',
'D000000007',
'CASHIE',
8.0,
0,
0,
'16:00 a',
'12:00 p',
'2016-02-04 00:00:00',
'2016-01-29 00:00:00'
);

Related

Inserting Date gives error ORA-01861: literal does not match format string

Insert date into WSH_Delivery_Details_Interface (https://docs.oracle.com/cloud/r13_update17c/scmcs_gs/OEDSC/WSH_DELIVERY_DETAILS_tbl.htm) throws this error
Query :
insert into WSH_DEL_DETAILS_Interface
(DELIVERY_DETAIL_INTERFACE_ID, CREATION_DATE, Date_Requested)
values
(30010985553,
TO_DATE('11/12/2018T05:10:30-00:00', 'DD/MM/YYYY '),
TO_DATE('11/12/2018', 'DD/MM/YYYY'));
Sample Record in creation_date and date_requested column:
Date_Requested Creation_Date
16-JUN-10 17-JUN-10 03.40.31.865000000 PM
The error can be reduced to:
select TO_DATE('11/12/2018T05:10:30-00:00','DD/MM/YYYY ') from dual;
Error report -
ORA-01861: literal does not match format string
Which is reasonable as it clearly doesn't match. You need to include the time elements in your format mask, and also a character literal for the fixed 'T', and for the fixed time zone offset:
select TO_DATE('11/12/2018T05:10:30-00:00','DD/MM/YYYY"T"HH24:MI:SS"-00:00"') from dual;
TO_DATE('11/12/2018
-------------------
2018-12-11 05:10:30
If that 'time zone' part isn't fixed and needs to be honoured, then you can use to_timestamp_tz() instead of to_date():
select TO_TIMESTAMP_TZ('11/12/2018T05:10:30-00:00','DD/MM/YYYY"T"HH24:MI:SS.FFTZH:TZM')
from dual;
TO_TIMESTAMP_TZ('11/12/20
-------------------------
2018-12-11 05:10:30.0 GMT
I've included .FF in the format model as well as the time zone offset elements (you could use TZR instead of TZH:TZM if you might be passed regions instead of offsets), since your example of existing data has fractional seconds, even though your literal string does not in this case
And you can cast() that to a date or plain timestamp if necessary, or possibly normalise to UTC if the input values can be other zones/offsets:
select TO_TIMESTAMP_TZ('11/12/2018T04:10:30-01:00','DD/MM/YYYY"T"HH24:MI:SS.FFTZH:TZM') as orig,
SYS_EXTRACT_UTC(
TO_TIMESTAMP_TZ('11/12/2018T04:10:30-01:00','DD/MM/YYYY"T"HH24:MI:SS.FFTZH:TZM')) as utc
from dual;
ORIG UTC
---------------------------- ---------------------
2018-12-11 04:10:30.0 -01:00 2018-12-11 05:10:30.0

How to convert oracle to_timestamp to sql?

I want to convert Oracle
to_timestamp(coloum_name,'DD-MM-YYYY') to sql
required output : 24-APR-17 12.00.00.000000000 PM
I know this is old, but it has an very searchable title, and there's no accepted answer.
The TO_TIMESTAMP function converts text representations of dates to a standard date/time format. From your question, it sounds like you have dates stored as characters in the format 'DD-MM-YYYY', but you want SQL Server DATETIME2(7) (based on the number of decimals in the seconds) as your output. It also seems you want the default time to be noon, rather than midnight, since your sample output shows 12:00 PM, not AM.
Using CONVERT with style 103 will change the European styled date to a DATETIME2(7), as shown below. But then you'll need to do a DATEADD to move from midnight (which will be the default value) to noon, which is twelve hours later.
DECLARE #DateSample NVARCHAR(10) = '17-04-2017';
SELECT CONVERT( DATETIME2(7), #DateSample, 103 );
--Results
--2017-04-17 00:00:00.0000000
SELECT DATEADD( HOUR, 12, CONVERT( DATETIME2(7), #DateSample, 103 ));
--Results
--2017-04-17 12:00:00.0000000
The SQL Server default is 24 hour time, so if you absolutely must switch to AM/PM designators, you'll have to convert it back to a string, which seems to be the opposite of what you're trying to do.
This is a way to convert a date/timestamp into varchar2 in Oracle with the format you want
select to_char(yourColumn, 'DD-MON-YY HH.MI.SS.FF9 PM')
from yourTable
SELECT FORMAT(SYSDATETIME(), 'dd-MMM-yyyy h.mm.ss.fffffff tt')

Using TO_DATE() with AM/PM Formatting

I am trying to select some dates from a table where the format of the dates is like this:
14-APR-14 10.35.00.0000000000 AM
01-NOV-16 02.43.00.0000000000 PM
Note that the dates can be either AM or PM, but when I try to do a simple SELECT from the table such as:
SELECT * FROM MyTable
WHERE TO_DATE(MyDate, 'DD-MON-YYYY HH:MI:SS AM') > '31-DEC-2016 08:00:00 AM';
I get the error:
ORA-01855: AM/A.M. or PM/P.M. required
I've been trying to get this work for some time but with no luck. Any help here would be appreciated.
Several problems.
Your inputs are obviously strings, since they have ten decimal places and timestamps in Oracle have at most 9. Then, strings with fractions of a second can't be converted to a date with to_date - you need to use to_timestamp or else you need to remove all the fractional parts. In the solution below I only remove the last (the tenth) decimal, since you may have non-zero fractional parts in the table - although not in the sample you posted.
Then, your format mask has yyyy but your inputs have only two digits for the year (which probably means 93 means 1993 and not 2093, so the correct thing to use would be rr rather than yy). And you use : in the format mask where your inputs use .
Finally, don't even compare dates in string format: in string comparisons, 01-JAN-2015 is before 20-NOV-2013.
You probably want something like this:
select mydate
from (
select '14-APR-14 10.35.00.0000000000 AM' as mydate from dual
union all
select '01-NOV-16 02.43.00.0000000000 PM' from dual
) mytable
where to_timestamp(substr(mydate, 1, 28) || substr(mydate, -3), 'dd-MON-rr hh.mi.ss.ff AM')
> to_timestamp('31-DEC-2016 08:00:00 AM', 'dd-MON-yyyy hh:mi:ss AM');
This query compiles correctly, and it produces no rows in the output (for obvious reasons).
NOTE: In a comment you (the OP) say the mydate field is a timestamp(6) datatype. Hard to believe (you show ten decimal places), but if indeed it is a timestamp or date, then you don't need to wrap it within any to_timestamp or to_date function, it should stand alone in the left-hand side of the inequality.
From your comment:
It's actually a timestamp; not a string. Timestamp(6) to be precise
You can just use a TIMESTAMP literal:
SELECT *
FROM MyTable
WHERE MyDate > TIMESTAMP '2016-12-31 08:00:00';

T-SQL : convert(datetime) to include/exclude certain hours

Using date range to select values, but also need to use an hour range to determine if a record should be selected. The date ranges and time ranges are not necessarily associated.
game_time (between 6 am and 6 pm)
have tried straight between statement and datepart, but cannot get anything to capture what we need.
create table gametime(name varchar, start_time datetime, end_time datetime)
insert assorted name, start_times and end_times
Desired results
name start_time end_time
name1 8:00 AM 10:00 AM
name2 8:00 AM 11:30 AM
name3 4:00 PM 5:30 PM
name4 6:00 PM 9:00 PM
datetime is used is storage, but not needed in presentation.. only times are needed in presentation.
Selected games should only start between the hours of 6:00 AM and 6:00 PM.
Any and all suggestions and insight appreciated......
Using
LTRIM(RIGHT(CONVERT(VARCHAR(20), start_time, 100), 7))
to get the correct format for presentation,
but when I try to use
LTRIM(RIGHT(CONVERT(VARCHAR(20), start_time, 100), 7)) > 6
I get conversion errors.
I would use DATEPART rather than relying on converting to/comparing strings:
WHERE DATEPART(hour,start_time) BETWEEN 6 AND 18
Try CONVERT(VARCHAR(5),start_time,108) BETWEEN '06:00' AND '18:00'. Right now you're trying to compare a string to an integer.
Here's another way, provided you're on SQL Server 2008 or higher and have the TIME type available:
SELECT *
FROM gametime
WHERE CAST(start_time AS TIME) BETWEEN '06:00:00' and '18:00:00'
This can be a bit more flexible when your time range is not anchored to exact hours. It also is sarg-able -- i.e. it will use an index, where calling DATEPART will prevent that.

Is a date in Oracle-SQL without a time-value always 00:00:00?

Heyho,
I need to grab some datas from actions which been done from date A 00:00:00 and date B 00:00:00 (in this case
Date A: 16.07.2010
Date B: 20.07.2010)
so i wrote this select-statement:
Select avg(cnt),fext from (
Select
to_char(mytable.dateadded, 'DD.MM.YYYY') dateadded,
fext,
count(id) cnt
from mytable
where dateadded between
to_date('16.07.2010', 'dd,MM,YYYY') and
to_date('20.07.2010', 'dd,MM,YYYY')
group by
to_char(mytable.dateadded, 'DD.MM.YYYY'),
fext)
group by fext;
The original (and working) statement had:
to_date('16.07.2010 00:00:00', 'dd,MM,YYYY HH24:Mi:SS') and
to_date('20.07.2010 00:00:00', 'dd,MM,YYYY HH24:Mi:SS')
so the question is: does the
to_date('16.07.2010', 'dd,MM,YYYY') and
to_date('20.07.2010', 'dd,MM,YYYY')
already set the time to date A and B to 00:00:00?
Greetz
If you does not specify time part of date it will be 00:00:00.
If you worry about time part you always can truncate time part:
Trunc(to_date('16.07.2010', 'dd.MM.YYYY')) and
Trunc(to_date('20.07.2010', 'dd.MM.YYYY'))
The easiest way is to use ANSI date literals, which doesn't allow time to be specified and is thus with a 00:00:00 time part internally:
dateadded between date '2010-07-16' and date '2010-07-20'
Your expressions look odd, as your date string has dots as the time component separator, but your date format has commas. Oracle accepts it though:
rwijk#XE> select to_date('16.07.2010', 'dd,MM,YYYY') from dual;
TO_DATE('16.07.2010
-------------------
16-07-2010 00:00:00
1 row selected.
I'd use the ANSI date literal for its simplicity.
Regards,
Rob.
this query will returns you one row which answers your question i think :
SELECT TO_DATE('16.07.2010 00:00:00', 'dd,MM,YYYY HH24:Mi:SS'),
TO_DATE('16.07.2010', 'dd,MM,YYYY')
FROM dual
WHERE to_date('16.07.2010 00:00:00', 'dd,MM,YYYY HH24:Mi:SS') = to_date('16.07.2010', 'dd,MM,YYYY')
This is one thing that SQL is consistent about - if you don't provide a time portion on a DATE data type that includes a time portion (IE Oracle, PostgreSQL while MySQL and SQL Server explicitly call it DATETIME), then the time portion is defaulting to exactly midnight of that date - 00:00:00. And being midnight, that means the start of the date - if you want to include the entire date in a date comparison, the value's time portion needs to be 23:49:49 -- a second to midnight.
23:59:59 is a second to midnight. Essentially midnight is defined as the start of a day: 00:00:00. Then the seconds clock up to the end of the day.