I have following table in Oracle:
DESC TIME_PERIOD
Name Null Type
---------- -------- ------------
TIME_ID NOT NULL NUMBER(2)
START_TIME NOT NULL TIMESTAMP(6)
END_TIME NOT NULL TIMESTAMP(6)
I am inserting values, but values are not inserted. I am using the following query.
INSERT INTO TIME_PERIOD (TIME_ID,START_TIME,END_TIME)
VALUES (1, TO_DSINTERVAL('0 23:59:59'), TO_DSINTERVAL('0 23:59:59'));
How can I insert value in Oracle?
I want time like this
10:00 Am
11:00Am
1:00pm
Not quite sure what you're trying to do, but to insert a TIMESTAMP you need to use the SYSTIMESTAMP() function:
e.g.
INSERT INTO TIME_PERIOD (START_TIME)
VALUES (SYSTIMESTAMP);
Related
I set or insert time in a postgres table value with now()::timestamp
But it does not work in oracle db.
This is the table:
create table types
(
id number not null,
description varchar(255) not null,
created_at timestamp null,
updated_at timestamp null,
deleted_at timestamp null,
CONSTRAINT autenticacion_id PRIMARY KEY (id)
);
So to insert the data I make:
insert into types (id, description) values (1, 'hello world', now()::timestamp)
But I get:
ORA-00917: missing comma
insert into types (id, description) values (1, 'hello world', (select sysdate from dual))
I get:
ORA-00942: table or view does not exist
You are providing 3 values and have only given 2 columns that you want to insert into.
SYSDATE is a DATE data type and SYSTIMESTAMP is a TIMESTAMP data type; they both have a date and time component but SYSTIMESTAMP also has fractional seconds (and a time zone, but that will be ignored as the column you are inserting into is a TIMESTAMP and not a TIMESTAMP WITH TIME ZONE data type).
What you probably want is:
INSERT INTO types (id, description, created_at)
VALUES (1, 'hello world', SYSTIMESTAMP)
However, you may want to consider that the timestamp should be in a specific time zone (typically UTC):
INSERT INTO types (id, description, created_at)
VALUES (1, 'hello world', SYSTIMESTAMP AT TIME ZONE 'UTC')
Note: You can wrap a value in (SELECT value FROM DUAL) but it is not necessary.
fiddle
The standard date format in any oracle table is DD-MON-YY, but I still wonder for the below query if the date will get stored into reg_date column in 'DD-MON-YY' format as it is the Oracle standard or will it get stored as per the 'FMmonth DD, YYYY' format?
insert into table (id,name,reg_date)
values (1, 'abc', TO_CHAR(SYSDATE,'FMmonth DD, YYYY') );
To directly answer your question, if the column REG_DATE is (as it should be) defined as a DATE, then it will, as all of the comments have said, be stored in oracle's internal binary format for DATEs.
And you supplied vaue of
TO_CHAR(SYSDATE,'FMmonth DD, YYYY')
will simply force in implied TO_DATE() function on the string that results from your use of TO_CHAR. That implied TO_DATE will use the format mask defined by the controlling setting of NLS_DATE_FORMAT. And if that mask does not match what you used in your TO_CHAR (and that is very unlikely) you will get an error.
SQL> create table my_table (id number,
2 fname varchar2(10),
3 reg_date date
4 );
Table created.
SQL>
SQL> insert into my_table (id,fname,reg_date)
2 values (1, 'abc', TO_CHAR(SYSDATE,'FMmonth DD, YYYY') );
values (1, 'abc', TO_CHAR(SYSDATE,'FMmonth DD, YYYY') )
*
ERROR at line 2:
ORA-01858: a non-numeric character was found where a numeric was expected
SQL> --
SQL> insert into my_table (id,fname,reg_date)
2 values (1, 'abc', SYSDATE);
1 row created.
SQL> --
SQL> select id,
2 fname,
3 reg_date,
4 to_char(reg_date,'FMmonth DD, YYYY') date1,
5 to_char(reg_date,'dd-MON-yyyy') date2,
6 to_char(reg_date,'yyyy-mm-dd hh24:mi:ss') date3
7 from my_table
8 ;
ID FNAME REG_DATE DATE1 DATE2 DATE3
--- ----- --------- ----------------- ----------- -------------------
1 abc 18-FEB-21 february 18, 2021 18-FEB-2021 2021-02-18 12:08:45
1 row selected.
Lets see what Oracle actually does when you insert into a date column using various formats: ISO Standard, Oracle's NLS_DATE_FORMAT specification, and a format I just made up. Then a couple queries, and finally, with the DUMP function, a peek inside. (Also see here for slightly different set.)
create table date_examples( date_1 date, date_2 date, date_3 date);
alter session set nls_date_format = 'dd-Mon-yyyy'; -- set default
-- set the same date in verious formats: ISO Standare, Declared Standard, a strange format
insert into date_examples( date_1, date_2, date_3)
select date '2021-02-18' -- standard iso format
, to_date('18-Feb-2021') -- defaulr see alter session
, to_date('18 2021 02', 'dd yyyy mm') -- specified
from dual;
-- what are the various dates without specifying how
select * from date_examples;
-- now change the default
alter session set nls_date_format = 'Month dd, yyyy hh24:mi:ss'; -- set default
select * from date_examples;
-- take a peek at the inside.
select dump(date_1), dump(date_2), dump(date_3)
from date_examples;
i have table with timestamp column
i want to add on each insert
to add the current timestamp + 30 minutes
how to achieve that
im using oracle 11g;
As you have a timestamp column it's probably better to use systimestamp instead of sysdate, and add an interval rather than a fraction of a day - which would lose the fractional second precision (and time zone, if your column actually stores that too).
You would either just have a date (if you use sysdate + 30/1440) or implicitly convert to a date (if you use systimestamp + 30/1440); and either way will you'll end up with a date that is then implicitly or explicitly converted to a timestamp as it's stored in your column.
As a simple example of using an interval:
create table t42 (col1 number, col2 timestamp);
create trigger tr42
before insert on t42
for each row
begin
:new.col2 := systimestamp + interval '30' minute;
end;
/
select systimestamp from dual;
SYSTIMESTAMP
---------------------------------
2019-02-13 07:17:11.971542000 GMT
insert into t42 (col1) values (42);
select col1, col2 from t42;
COL1 COL2
---------- -----------------------------
42 2019-02-13 07:47:12.253603000
You could also use a default value for the column instead of a trigger:
create table t42 (
col1 number,
col2 timestamp default systimestamp + interval '30' minute
);
select systimestamp from dual;
SYSTIMESTAMP
---------------------------------
2019-02-13 07:17:12.962268000 GMT
insert into t42 (col1) values (42);
select col1, col2 from t42;
COL1 COL2
---------- -----------------------------
42 2019-02-13 07:47:13.028670000
although that does allow the person doing the insert to provide their own value:
insert into t42 (col1, col2) values (43, timestamp '2000-01-01 00:00:00.0');
select col1, col2 from t42;
COL1 COL2
---------- -----------------------------
42 2019-02-13 07:47:13.028670000
43 2000-01-01 00:00:00.000000000
The trigger would override any user-supplied value (though it could also be modified not to.)
You could also use current_timestamp instead of systimestamp - they do slightly different things.
you can create a trigger as shown below. this will insert current timestamp+30 minutes each time you insert a row to the table.
create or replace trigger before_insert
before insert
on table_name for each row
declare
v_time date;
begin
select sysdate+30/1440 into v_time from dual;
:new.cur_time :=v_time;
end;
/
I have table like below
ID StartDate EndDate
1 15-MAR-2009 8-DEC-2011
2 14-JAN-2010 18-FEB-2013
I need output something like this -
ID StartDate EndDate
1 15-MAR-2009 31-DEC-2009
1 1-JAN-2010 31-DEC-2010
1 1-JAN-2011 8-Dec-2011
2 14-JAN-2010 31-DEC-2010
2 1-JAN-2011 31-DEC-2011
2 1-JAN-2012 31-DEC-2012
2 1-JAN-2013 18-FEB-2013
I have 9i oracle as database, I would perfer to do this with informatica but seems difficult to find solution there, cant create a procedure so need sql query. If anyone can provide in informatica 8.6 then that would be even great.
Edited :-
Create table test_range
(Sr_No number,
start_date date,
end_date date)
create table yr_range
(years date);
Insert statements :-
insert into test_range values (1,'15-MAR-2009','8-DEC-2011');
insert into test_range values (2,'14-JAN-2010','18-FEB-2013');
insert into yr_range values ('31-Dec-2005');
insert into yr_range values ('31-Dec-2006');
insert into yr_range values ('31-Dec-2007');
insert into yr_range values ('31-Dec-2008');
insert into yr_range values ('31-Dec-2009');
insert into yr_range values ('31-Dec-2010');
insert into yr_range values ('31-Dec-2011');
insert into yr_range values ('31-Dec-2012');
insert into yr_range values ('31-Dec-2013');
As suggested in one of the answer, I have created another table to get desired output, is it possible using one table only(i.e. test_range)? I m want exploer all the options before finalizing the one.
If you had something like a years dimension, where each row represented a year, then you would join with it based on whether the year falls within the range. This will give you the number of rows you need, and then for each row you conditionally output the start/end ranges. This should also handle cases where your range is less than a year and thus would not be split up.
y.Year is assumed to be an integer.
See working example here: http://sqlfiddle.com/#!4/d4589/13/11
Create table test_range
(Sr_No number,
StartDate date,
EndDate date);
create table yr_range
(years number);
insert into test_range values (1,'15-MAR-2009','8-DEC-2011');
insert into test_range values (2,'14-JAN-2010','18-FEB-2013');
insert into yr_range values ('2005');
insert into yr_range values ('2006');
insert into yr_range values ('2007');
insert into yr_range values ('2008');
insert into yr_range values ('2009');
insert into yr_range values ('2010');
insert into yr_range values ('2011');
insert into yr_range values ('2012');
insert into yr_range values ('2013');
select
r.Sr_No,
CASE
When to_char( r.StartDate, 'yyyy') = y.years Then r.StartDate
Else to_date( y.years || '/01/01', 'yyyy/mm/dd')
END as StartDate,
CASE
When to_char( r.EndDate , 'yyyy') = y.years Then r.EndDate
Else to_date( y.years || '/12/31', 'yyyy/mm/dd')
END as EndDate
from test_range r, yr_range y
where
to_char( r.StartDate, 'yyyy') <= y.years
AND to_char( r.EndDate, 'yyyy') >= y.years;
You could also generate your years sequence using some of these techniques:
ORACLE SQL:Get all integers between two numbers
I have col1 in myTable which is varchar, and I have to insert here timestamp eg:- 09-MAY-11 10.23.12.0000 AM.
Now please tell me:
How to insert into myTable with taking sysdate in above format...
How to retrieve data from col1 in tha same timestamp format..
INSERT:
insert into myTable (col1) VALUES (to_char(systimestamp, 'dd-mon-yyyy hh.mi.ss.ff4 AM') );
SELECT:
select to_timestamp(col1, 'dd-mon-yyyy hh.mi.ss.ff4 AM') from myTable ;
But it is much better to store the data directly as a timestamp.
Then you can compare the values or modify them directly.
create table myTable1( col1 timestamp default systimestamp);