insert statment make error - sql-insert

I am using oracle 12c with the username system. My problem is when I execute this insert statement that I took from oracle live sql site:
insert into emp
values(7788, 'SCOTT', 'ANALYST', 7566,to_date('13-JUL-87','dd-mm-rr') - 85,3000, null, 20);
it shows :
sql error ora-01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
what is this -85 after the to_date(..)

To handle dates, you would better use the ANSI format (date 'yyyy-mm-dd'):
insert into emp values(7788, 'SCOTT', 'ANALYST', 7566, date '1987-07-13'- 85,3000, null, 20);
If you need to use a to_date for some reason, you have to be sure that the format of your string exactly matches the format mask you use: if your month is written as 'JUL' you need 'MON' in the format mask and not 'mm'. 'mm' would match a month written as '07'.
Please notice that even with the right format mask, this way to write dates is dangerous, because it's based on the language of your DB.
The -85 means "subtract 85 days".

Related

Converting a varchar timestamp (with possible single digit date or month) to datetime

I have to convert timestamp which is in varchar (3/12/2021 1:38:53 PM) to datetime using SSIS. This is code I used to do the same
(DT_DBDATE)(SUBSTRING(TimeStamp,FINDSTRING(TimeStamp,"/",2),4) + "-" + (LEN(SUBSTRING(TimeStamp,FINDSTRING(TimeStamp,"/",1),FINDSTRING(TimeStamp,"/",2)))==1?"0"+SUBSTRING(TimeStamp,FINDSTRING(TimeStamp,"/",1),FINDSTRING(TimeStamp,"/",2)):SUBSTRING(TimeStamp,FINDSTRING(TimeStamp,"/",1),FINDSTRING(TimeStamp,"/",2))) + "-" + (LEN(SUBSTRING(TimeStamp,1,FINDSTRING(TimeStamp,"/",1)))==1?"0"+SUBSTRING(TimeStamp,1,FINDSTRING(TimeStamp,"/",1)):SUBSTRING(TimeStamp,1,FINDSTRING(TimeStamp,"/",1))))
The above threw the following error:
[Derived Column [2]] Error: An error occurred while attempting to
perform a type cast.
I would also need to include the time as my destination SQL column is DATETIME2.
Without manual parsing method:
From microsoft docs:
When a string is cast to a DT_DATE, or vice versa, the locale of the transformation is used.
Go into derived column advanced setting and set LocaleId to something appropriate for your date source. LocaleId of "English (United States)" should work based on the example you provided.
Then leverage DT_DATE to do the parsing before casting it to DT_DBDATE
(DT_DBDATE)((DT_DATE)TimeStamp)

In Cognos 10.2.1 - how can I use substring portions of an input parameter to create an expression to use for date comparison?

In Cognos 10.2.1 (FP7), I have a situation with two tables (see below), where a given transaction may be in the same "PERIOD", but what I want to do is to only give me results for transactions that have a date after the 15th of the month (i.e - the transaction may be in PERIOD 201510, and I only want transactons on/after 10/15/2015). The PERIOD is an input parameter that the user selects - I want to build the date from a portion of the PERIOD. (Before anyone complains about syntax, etc. - I've tried to simplify this as much as possible).
Given Table A:
ID varchar
PERIOD varchar
Given Table B:
ID varchar
TYPE varchar
TRANDATE timestamp2
Query1
[A.PERIOD]=?PARM1? <- this is the user selected input parameter
Query2
[B.TYPE] in ('A','B')
Then create a join from the results of Query1 and Query2 on the A.ID=B.ID to give Results1
I've tried the following (I'm keeping this as simple as I can - so I'm ONLY working with the YEAR portion of the period):
[Results1].[TRANDATE] >= concat(substring([Results1].[PERIOD],1,4),'-10-15T00:00:00.000000000')
[Results1].[TRANDATE] >= cast(concat(substring([Results1].[PERIOD],1,4),'-10-15T00:00:00.000000000'), timestamp2)
In both cases - Cognos won't validate the expression, or, if it does validate, I get a runtime error when running the report. In both cases, I get messages basically "literal does not match format string", even with the cast.
So - how can I get pieces/portions of the parameter, and slice/dice them to use as a date comparison as I mentioned above?
Given: Column [PERIOD] of type integer in the form YYYYMM, [TRANDATE] of type date/time
[TRANDATE] >= cast(cast(cast([PERIOD]/100,integer),varchar(4)) || '-' || cast(mod([PERIOD],100),varchar(2)) || '-15',date)
Given: Column [PERIOD] of type string in the form 'YYYYMM', [TRANDATE] of type date/time
[TRANDATE] >= cast(substring([PERIOD],1,4) || '-' || substring([PERIOD],5,2) || '-15',date)
The trick is that you don't have to build a full date/time string. You only have to build a string of format 'YYYY-MM-DD' in order to cast to a date type.

Fastload to temporal table

Had a cvs file containing 3 fields
1,cat,2012-06-16,2013-06-16
1,cat,2013-06-16,
I am trying to load that to temporal table having valid_dt PERIOD(DATE) using fastload script
nonsequenced validtime
INSERT INTO financial.test1 (id,name,valid_dt) values
(:id,:name,period( cast(:start_dt as date FORMAT 'YYYY-MM-DD'),cast(:end_dt as date FORMAT 'YYYY-MM-DD'))
);
Error I got is RDBMS error 3618: Expression not allowed in Fast Load
Insert, column INTERNALPERIODDATETYPE.
could not find any in manuals, they only said it will be possible with fastload.
Thankyou.
FastLoad doesn't allow ANSI style CAST, must be old Teradata style instead:
:start_dt (date, FORMAT 'YYYY-MM-DD')
But there's no old-style PERIOD cast and FastLoad also doesn't allow any kind of expression and PERIOD(...) is an expression.
So you can only load data which can be automatically converted to a PEROD like:
1;cat;(2012-06-16, 2013-06-16)
1;cat;(2013-06-16, 9999-12-31)
Including the parens, the blank after the comma and a different delimiter...
I would suggest simply loading the data as DATEs (or CHARs) into a staging table using FastLoad or MultiLoad, followed by a
nonsequenced validtime
INSERT INTO financial.test1 (id,name,valid_dt) values
select id, name, period(start_dt,COALESCE(end_dt, date '9999-12-31'))
from stagingtable

Inserting a Datetime value into SQL Server CE in Webmatrix 3

I want to insert a datetime value into a database using SQL Server Compact Edition in Microsoft Webmatrix 3.
I tried the following query:
INSERT INTO Tutorials ([Tutorial], [StartDate])
VALUES ('3d', CONVERT(DATETIME, '07-23-08', 110));
And I got the following error message:
The conversion is not supported. [ Type to convert from (if known) = datetime, Type to convert to (if known) = float ]
Try with
INSERT INTO Tutorials ([Tutorial], [StartDate])
VALUES ('3d', CONVERT(DATETIME, '07-23-08', 10));
If you set the style value to 10 the input format must be mm-dd-yy, if you add 100 to the style value the expected format has a four digit year (110 --> mm-dd-yyyy).
For an exhaustive table of the style values look at CAST and CONVERT (SQL Server Compact).
By the way, you could take advantage of an implicit conversion using a date format like yyyymmdd or yyyy-mm-dd:
INSERT INTO Tutorials ([Tutorial], [StartDate])
VALUES ('3d', '20080723');

SQLite less than operator problem

I am using SQLite for a project and < symbol is not working in the query.
There is a table named Holidays which has a field of datatype datetime.
Suppose the table contains some dates of current year in the column HolidayDate.
SELECT HolidayDate
FROM Holidays
WHERE (HolidayDate >= '1/1/2011')
AND (HolidayDate <= '1/1/2012')
The < symbol in the above query is not working. > symbol in the above query is working well.
Please help me.
Try:
SELECT HolidayDate
FROM Holidays
WHERE HolidayDate >= date('2011-01-01')
AND HolidayDate <= date('2012-01-01')
(date format must be YYYY-MM-DD)
There is no datetime datatype in sqlite.
Sqlite only has 4 types:
integeral number
floating-point number
string (stored either as utf-8 or utf-16 and automatically converted)
blob
Moreover, sqlite is manifest-typed, which means any column can hold value of any type. The declared type is used for two things only:
inserted values are converted to the specified type if they seem to be convertible (and it does not seem to apply to values bound with sqlite_bind_* methods at all)
it hints the indexer or optimizer somehow (I just know it has trouble using indices when the column is not typed)
Even worse, sqlite will silently accept anything as type. It will interpret it as integeral type if it starts with "int", as string if it contains "char" or "text", as floating-point number if it is "real", "double" or "number" and as blob if it's "blob". In other cases the column is simply untyped, which poses no problem to sqlite given how little the typing means.
That means '1/1/2011' is simply a string and neither dates in format 'mm/dd/yyyy' nor dates in format 'dd/mm/yyyy' sort by date when sorted asciibetically (unicodebetically really).
If you stored the dates in ISO format ('yyyy-mm-dd'), the asciibetical sort would be compatible with date sort and you would have no problem.

Resources