Inserting a Datetime value into SQL Server CE in Webmatrix 3 - datetime

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

Related

update or convert a date time value in a sqlite database

I have a SQLite database with a TEXT column that stores a DateTime value formated like this: '11/08/2019 00:00:00'. I would like to convert the entire column contents to UTC Epoch timestamp local timezone.
Is there an Update SQL string with a DateTime function that I could run using supported SQL syntax to perform this task or should I perhaps just write a quick C# console application to do it?
I have not found any example online or in SO that would do what I need to do in this situation.
An UPDATE SQL such as :-
UPDATE mytable SET mycolumn =
CASE WHEN substr(mycolumn,3,1) = '/'
THEN
strftime('%s',substr(mycolumn,7,4)||'-'||substr(mycolumn,4,2)||'-'||substr(mycolumn,1,2)||' '||substr(mycolumn,12,8))
ELSE
mycolumn
END
;
could be used.
Example
Perhaps consider the following which will convert the column (or not if it has already been converted (or not if it does not match the dd/mm/yyyy format))
Note the below just checks the 3rd character for /, a more rigorous check could be used if desired.
:-
DROP TABLE IF EXISTS mytable;
CREATE TABLE IF NOT EXISTS mytable (mycolumn TEXT);
/* Load the testing data */
INSERT INTO mytable VALUES
('11/08/2019 00:00:00'),
('01/08/2019 00:00:00'),
('31/01/2019 00:00:00'),
('31/01/2019 13:25:33.004') /* test for micro seconds (dropped by utc)*/;
/* display data before conversion */
SELECT * FROM mytable;
/* Convert the data to unix */
UPDATE mytable SET mycolumn =
CASE WHEN substr(mycolumn,3,1) = '/'
THEN
strftime('%s',substr(mycolumn,7,4)||'-'||substr(mycolumn,4,2)||'-'||substr(mycolumn,1,2)||' '||substr(mycolumn,12,8))
ELSE
mycolumn
END
;
/* Display data as is, as formatted localised and as formatted UTC */
SELECT *, datetime(mycolumn,'unixepoch','localtime') AS local, datetime(mycolumn,'unixepoch') AS utc FROM mytable;
Note the above would NOT cater for dates such as 1/1/2019, such dates would need a more complex CASE clause.
Note that UTC is worldwide coordinated time i.e one value is stored you adjust from UTC according to the time zone
Results
Note testing in timezone that is +10 hours
When first run the results are :-
Pre-conversion :-
Post-convserion
Rerun (DROP commented out)
Pre-conversion (mixed data) :-
circled data is already converted
Post-conversion :-

Can't convert nvarchar to datetime

I have cells like 'Dec 6 2016 6:26AM' (copied from DB) in NVARCHAR. I want to convert that to the Datetime format . But every time when I try do that there are the error 'Error converting data type nvarchar to datetime'.
I do not understand what is the problem, because my cell is absolutely similar to Datetime 100 format.
For deciding the problem I have alreade tried:
convert and cast (do not work), Try_convert and Try_ cast do not work too (get me nulls only), creating a new table with needed column format and inserting into that my data and etc.
The exprected result is opportunity to use DATEDIFF function
Have you tried this:
SELECT CONVERT(DATETIME,'Dec 6 2016 6:26AM',100);
gives the result:
2016-12-06 06:26:00.000

insert statment make error

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".

PostgreSQL, R and timestamps with no time zone

I am reading a big csv (>1GB big for me!). It contains a timestamp field.
I read it (100 rows to start with ) with fread from the excellent data.table package.
ddfr <- fread(input="~/file1.csv",nrows=100, header=T)
Problem 1 (RESOLVED): the timestamp fields (called "ts" and "update"), e.g. "02/12/2014 04:40:00 AM" is converted to string. I convert the fields back to timestamp with lubridate package mdh_hms. Splendid.
ddfr$ts <- data.frame( mdy_hms(ddfr$ts))
Problem 2 (NOT RESOLVED): The timestamp is created with time zone as per POSIXlt.
How do I create in R a timestamp with NO TIME ZONE? is it possible??
Now I use another (new) great package, PivotalR to write the dataframe to PostGreSQL 9.3 using as.db.data.frame. It works as a charm.
x <- as.db.data.frame(ddfr, table.name= "tbl1", conn.id = 1)
Problem 3 (NOT RESOLVED): As the original dataframe timestamp fields had time zones, a table is created with the fields "timestamp with time zone". Ultimately the data needs to be stored in a table with fields configured as "timestamp without time zone".
But in my table in Postgres the data is stored as "2014-02-12 04:40:00.0", where the .0 at the end is the UTC offset. I think I need to have "2014-02-12 04:40:00".
I tried
ALTER TABLE tbl ALTER COLUMN ts type timestamp without time zone;
Then I copied across. While Postgres accepts the ALTER COLUMN command, when I try to copy (using INSERT INTO tbls SELECT ...) I get an error:
"column "ts" is of type timestamp without time zone but expression is of type text
Hint: You will need to rewrite or cast the expression."
Clearly the .0 at the end is not liked (but why then Postgres accepts the ALTER COLUMN? boh!).
I tried to do what the error suggested using CAST in the INSERT INTO query:
INSERT INTO tbl2 SELECT CAST(ts as timestamp without time zone) FROM tbl1
But I get the same error (including the suggestion to use CAST aargh!)
The table directly created by PivotalR (based on the dataframe) has this CREATE script:
CREATE TABLE tbl2
(
businessid integer,
caseno text,
ts timestamp with time zone
)
WITH (
OIDS=FALSE
);
ALTER TABLE tbl1
OWNER TO mydb;
The table I'm inserting into has this CREATE script:
CREATE TABLE tbl1
(
id integer NOT NULL DEFAULT nextval('bus_seq'::regclass),
businessid character varying,
caseno character varying,
ts timestamp without time zone,
updated timestamp without time zone,
CONSTRAINT busid_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE tbl1
OWNER TO postgres;
My apologies for the convoluted explanation, but potentially a solution could be found at any step in the chain, so I preferred to put all my steps in one question. I am sure there has to be a simpler method...
I think you're confused about copying data between tables.
INSERT INTO ... SELECT without a column list expects the columns from source and destination to be the same. It doesn't magically match up columns by name, it'll just assign columns from the SELECT to the INSERT from left to right until it runs out of columns, at which point any remaining cols are assumed to be null. So your query:
INSERT INTO tbl2 SELECT ts FROM tbl1;
isn't doing this:
INSERT INTO tbl2(ts) SELECT ts FROM tbl1;
it's actually picking the first column of tbl2, which is businessid, so it's really attempting to do:
INSERT INTO tbl2(businessid) SELECT ts FROM tbl1;
which is clearly nonsense, and no casting will fix that.
(Your error in the original question doesn't match your tables and queries, so the details might be different as you've clearly made a mistake in mangling/obfuscating your tables or posted a newer version of the tables than the error. The principle remains.)
It's generally a really bad idea to assume your table definitions won't change and column order won't change anyway. So always be explicit about columns. In this case I think your intention might have actually been:
INSERT INTO tbl2(businessid, caseno, ts)
SELECT CAST(businessid AS integer), caseno, ts
FROM tbl1;
Note the cast, because the type of businessid is different between the two tables.

SQL Developer: Load Data Date error

I am using Oracle SQL Developer 3.0.03. I am trying to upload an Excel file to an Oracle data table. I am getting an error for the date. The column in the database is a timestamp and I don't know what to put into the date format for the 'Data Load Wizard'. If I use the following format (dd-mom-yy hh.mi.ss), SQL Developer will show the following error:
--Insert failed for row 1 TIMESTAMP_COLUMN GDK-05047: A day of the month must be between 1 and the last day of the month.
--Row 1
INSERT INTO TABLE_1 (Column1, Column2, Column3, TIMESTAMP_COLUMN) VALUES ('Some Text','Some Text','Some more text',to_timestamp('40604.0', 'dd-mon-yy hh.mi.ss'));
The default number format IN EXCEL is: 40604.0
Which if you change the cell to a short date format you will get: 3/2/2011
I am trying to figure out what 'Format' I need to put into the FORMAT section of the 'DATA Load Wizard' that will accept the date format that is in EXCEL and upload it to Oracle as a TIMESTAMP.
I ran into the same thing today, and 'fixed' this two ways. The second way probably seems too complex, but it might help someone if they have a hard time automating the formatting of dates to look like Oracle's standard dd-mmm-yy.
Format the date columns in Excel as dd-mmm-yy and import directly into the table.
Highlight the column(s)
Choose "More Number Formats" where the existing format is (In Excel 2010, it says General in a dropbox on the Home tab
Select the last entry "Custom" in the Category box
Manually enter dd-mmm-yy in the Type: box
Format the date columns in Excel as mm/dd/yy, import the table in as text, write a manual insert statement from the temp text table using TO_DATE(date_field,'MM/DD/YYYY')
Highlight the column(s)
Choose "More Number Formats" where the existing format is (In Excel 2010, it says General in a dropbox on the Home tab
Select the "Date" entry in the Category box
Choose "03/14/01" from the list
The Excel "zero" date is January 0 (yes, zero), 1900. Since Oracle can't handle a "zero" day in a month you need to subtract 1 from the Excel value; thus the following should get you close to what you want:
SELECT CAST(TO_DATE('01-JAN-1900 00:00:00', 'DD-MON-YYYY HH24:MI:SS') AS TIMESTAMP) +
NUMTODSINTERVAL(41017.6361109954 - 1, 'DAY')
FROM DUAL
As far as "how to import it" goes, there's no Oracle date format that I'm aware of to do this. The easiest thing would be to import the value into a NUMBER column and then run a script to do the date conversion.
Share and enjoy.
yeah and that's the problem.
"A day of the month must be between 1 and the last day of the month."
1) how are these decimals created?
2) is this "04-06-2004" ? or are these seconds from 1970?
you need to add more detail about this number format!

Resources