insert into not working on oracle in date field - database

I have created table as:
create table ot.eligible(
id number,
name varchar2(255),
join_date date,
left_date date
);
The problem I am getting is I am unable to insert the data which has date column:
insert into ot.eligible(ID,NAME,JOIN_DATE,LEFT_DATE) values(1,'ashwin',to_date(01/12/2017,'MM/DD/yyyy'),to_date(01/2/2018,'mm/dd/yyyy'));
before i tried without using to_Date and numeric found error was there so,I added to_date but,at this time I got error as:
ORA-01858: a non-numeric character was found where a numeric was expected
My sysdate is:
10/17/2019 8:42:29 PM
when I hitted select sysdate from dual;

You are missing single quotes in dates -
insert into ot.eligible(ID,
NAME,
JOIN_DATE,
LEFT_DATE)
values(1,
'ashwin',
to_date('01/12/2017','MM/DD/yyyy'),
to_date('01/02/2018','mm/dd/yyyy'))

Related

How to convert UNIX Epoch to date in Snowflake

My table INV_STOCK_TIME with 4000 rows with unix epoch data in snowflake. That data needs to be converted to date. Here is sample data and the desired output should be in 2 columns as given below
STOCK_RAWTIME
1617772221333
1616217315003
1601376748863
STOCK_RAWTIME STOCK_DATE
1617772221333 2021-04-07
1616217315003 2021-03-20
1601376748863 2020-09-29
I could to convert value in column using either of the below command.
select to_char(to_timestamp_tz(1617772221333,3),'YYYY-MM-DD') from dual;
OR
SELECT to_char(dateadd('ms',timestamp_col,'1970-01-01'),'YYYY-MM-DD') from (select 1617772221333 as timestamp_col) as timetest;
I would like to pass the values of column1 STOCK_RAWTIME (unix epoch value)
to populate column2 STOCK_DATE. I'm unable to pass list of values to convert column1 records with below error
Format argument for function 'TO_TIMESTAMP_TZ' needs to be a string
I believe you can get this requirement with the following:
SELECT STOCK_RAWTIME as STOCK_RAWTIME, to_date(STOCK_RAWTIME)AS STOCK_DATE FROM (select STOCK_RAWTIME from INV_STOCK_TIME);
Resolved with this step in snowflake as to_date or to_timestamp works fine:
alter table inv_stock_time add column stock_date date;
update inv_stock_time set stock_date=to_date(STOCK_RAWTIME);

Date Conversion in snowflake

I am facing issue in date conversion in snowflake
Tab1 : Date(DD-MM-YYYYY) Col1 : 03-10-2018
Tab2 : Date(YYYY-MM-DD) Col2 : 2018-10-03
I need to join
Tab1.Col1=Tab2.Col1
I am getting error as
Date '03-10-2018' is not recognized.
How to convert this date into 'YYYY-MM-DD' Please advise.
I have tried with all format :
TO_TIMESTAMP_NTZ(Date(DOS, 'YYYY-MM-DD')),
Date((DOS, 'YYYY-MM-DD'),
TO_Varchar(Date(DOS, 'DD-MM-YYYY'),'YYYY-MM-DD')
All above mentioned formula facing the same error :
Thanks in advance
I don't quite understand this part of your question if datatype of your columns tab1.col1 and tab2.col2 is date: Date(DD-MM-YYYYY) and Date(YYYY-MM-DD)
Date is date, it does not have any formats when it is saved. You apply formats when you insert dates in text format to tell in which format the given value is, or when you select the date and want to output it in some specific format.
This works with date-type columns, joining is done without any formatting functions:
create table tab1 (col1 date);
create table tab2 (col2 date);
insert into tab1 (col1) values ( to_date('03-10-2018','dd-mm-yyyy') );
insert into tab2 (col2) values ( to_date('2018-10-03','yyyy-mm-dd') );
select *
from tab1, tab2
where Tab1.Col1=Tab2.Col2;

SQL Server: combine date column and time column, insert into datetime column

I have a database with a date and a time stored separately in datetime columns (not my idea). I need to combine their values to put into another table with a datetime column. As simple as this seems, I just don't seem to be able to do it.
I can get my date value:
cast(sampledate as date) -- returns '2014-11-01'
And my date value:
cast(CollectionTime as time) -- returns '06:46:00.0000000'
I've tried a few different ways of putting them together that look OK.
For example:
concat(cast(sampledate as date) , ' ' , cast(CollectionTime as time)) -- returns '2014-11-05 08:14:00.0000000'
But when I try to insert this into a datetime column, or even just cast it as a datetime value, it doesn't work:
cast(concat(cast(sampledate as date) , ' ' , cast(CollectionTime as time)) as datetime)
-- I get the error 'Conversion failed when converting date and/or time from character string.'
This link warned me against using the FORMAT function, and I've been to some other sites that tell me what NOT to do, but I just can't seem to do this simple thing. Can anyone help? Thanks.
EDIT: Figured it out. This link solved it for older versions of SQL, but not current versions. However, it works fine if you cast to datetime2(0), not datetime.
As I commented above, here is an example where you can add the two datetimes together.
If either column is NOT datetime, simply convert that column to a datetime
Declare #YourTable table (sampledate datetime,CollectionTime datetime)
Insert Into #YourTable values
('2019-06-25 00:00:00','09:09:31')
Select *
,NewDateTime = sampleDate + CollectionTime
From #YourTable
Results
sampledate CollectionTime NewDateTime
2019-06-25 00:00:00.000 1900-01-01 09:09:31.000 2019-06-25 09:09:31.000

'[pervasive][ODBC Engine Interface]Invalid date, time or timestamp value' When trying to compare dates

I cant get past this error.
I have a date field stored in mmddyy format.
I want to only return records with a date after 12/31/2019.
I get that Pervasive uses yyyy-mm-dd by default.
I am reformatting the date with this code:
Convert(Concat('20',Concat(Right(Date,2),Concat('-',Concat(Left(Date,2),Concat('-',Substring(Date)))))),SQL_DATE)
A date value of 081820 returns 44061.
Convert('2019-12-31',SQL_DATE)
returns 43830.
And Convert(Concat('20',Concat(Right(Order_Header.Date_Shipped,2),Concat('-',Concat(Left(Order_Header.Date_Shipped,2),Concat('-',Substring(Order_Header.Date_Shipped,3,2)))))),SQL_DATE) > Convert('2019-12-31',SQL_DATE)
returns
[pervasive][ODBC Engine Interface]Invalid date, time or timestamp value
Ive tried cast and convert... anyone know what I am doing wrong in my And statement (in my where clause) or how I can get around this error?
You might have values that are not valid for a date in your data. You should probably check the records. The Date specification requires a month of 1 to 12, a day of 1 to 31 depending on the month (so Feb 30 would be invalid), and a year of 0000 to 9999. Something like this for months, you'd want to check days as well:
insert into sodate (f1) values ('442020');
insert into sodate (f1) values (' 2020');
select f1, left(f1,2) from sodate where left(f1,2) not in ('01','02','03','04','05','06','07','08','09','10','11','12')
The substring in the first example is missing parameters. Once you've got the date in the right format ('yyyy-mm-dd') you should be able to compare it to a date value without converting it to the SQL_DATE data type. One more thing, you're prepending '20' before all years, what happens if your data has dates from pre-2000? You'll need to account for that. In the PCC, when I run:
select Convert('2019-12-31',SQL_DATE)
I get:
EXPR_1
==========
12/31/2019
I used the folowing:
create table sodate (f1 char(6));
insert into sodate (f1) values ('123119');
insert into sodate (f1) values ('081818');
insert into sodate (f1) values ('081820');
insert into sodate (f1) values ('082020');
select * from sodate;
-- This should give the results you want
select concat(concat(concat(concat(concat('20',right(f1, 2)),'-'),left(f1,2)),'-'),substring(f1,3,2)) from sodate where
concat(concat(concat(concat(concat('20',right(f1, 2)),'-'),left(f1,2)),'-'),substring(f1,3,2)) > '2019-12-31';
-- This query shows you don't need the CONVERT to compare a date.
select concat(concat(concat(concat(concat('20',right(f1, 2)),'-'),left(f1,2)),'-'),substring(f1,3,2)) from sodate where
concat(concat(concat(concat(concat('20',right(f1, 2)),'-'),left(f1,2)),'-'),substring(f1,3,2)) >= now()
And got the following results:
EXPR_1
==============
2019-12-31
2020-08-18
and the second query gives:
EXPR_1
==============
2020-08-20

Automatic adding current time in SQL Server 2012

I have table with a column of datatype time(4).
When I'm inserting values into the table I need to auto insert just the current time with milliseconds (without date) into that column. I have tried with time stamp, date time... but without any success.
If you want to get the current time in SQL Server 2012, just use the CAST operator to achieve this:
SELECT
CAST(SYSDATETIME() AS TIME(4))
This will get the current date & time, and cast this to just the time, as you need it.
To achieve automatic entry of time values when a new row is added, you can use the above expression in a default constraint. For example:
DECLARE #T AS table
(
SomeValue integer NOT NULL,
TheTime time(4) NOT NULL
DEFAULT CAST(SYSDATETIME() AS time(4))
);
INSERT #T (SomeValue)
VALUES (1);
SELECT
SomeValue,
TheTime
FROM #T;
As far as I understand, this is what you may be looking for:
SELECT GETDATE() 'Today',
CONVERT(TIME(4),GETDATE()) 'time_only'
You could use the above conversion in your insert statement as following:
INSERT INTO TABLEA (
column1
,column2
,record_insert_time
)
VALUES (
value1
,value2
,CONVERT(TIME(4), GETDATE())
);

Resources