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);
Related
I am trying following query to insert data from a file. How do I calculate TOTAL_HOURS from in_date and out_date? I know we can not select them as "as in_date" and use it later. It was for illustration purpose only.
The script is reading data from a file and it is not stored in database yet.
insert into Table_A (
Id,
IN_DATE,
OUT_DATE,
TOTAL_HOURS,
)
values (
1,
'2021-10-04 07:00:00' as in_date,
'2021-10-04 07:00:00' as out_date,
DATEDIFF(second, out_date, in_date) / 3600.0
);
If you want to INSERT a static datetime using TIMESTAMPDIFF:
INSERT INTO Table_A (Id, IN_DATE, OUT_DATE, TOTAL_HOURS)
SELECT 1, '2021-10-04 07:00:00', '2021-10-04 08:00:00',
TIMESTAMPDIFF(HOUR, '2021-10-04 07:00:00', '2021-10-04 08:00:00')
If you want to INSERT a dynamic datetime using data from another table (i.e. Table_B to Table_A using a key):
INSERT INTO Table_A (Id, IN_DATE, OUT_DATE, TOTAL_HOURS)
SELECT Id, IN_DATE, OUT_DATE,
TIMESTAMPDIFF(HOUR, IN_DATE, OUT_DATE) FROM Table_B WHERE Id = 1
Result:
Id
IN_DATE
OUT_DATE
TOTAL_HOURS
1
2021-10-04 07:00:00
2021-10-04 08:00:00
1
Fiddle here.
To INSERT from a .csv file use the LOAD DATA INFILE command and add the TIMESTAMPDIFF calculation to the SET clause:
LOAD DATA INFILE 'c:/tmp/discounts_2.csv'
INTO TABLE Table_A
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(Id,#IN_DATE,#OUT_DATE)
SET IN_DATE=#IN_DATE,
OUT_DATE=#OUT_DATE,
TIMESTAMPDIFF(HOUR, #IN_DATE, #OUT_DATE);
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 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);
I need to get a row of information with max timestamp in postgresql. Below is a demo for this question:
drop table Mytable cascade
create table MyTable (usr char(1), event_dt timestamp without time zone);
insert into mytable values ('A','01-JAN-2009 11:10:11');
insert into mytable values ('A','02-JAN-2009 11:10:22');
insert into mytable values ('B','02-JAN-2009 01:01:59' );
insert into mytable values ('C', '31-DEC-2008 02:02:02');
insert into mytable values ('D', '31-DEC-2008 03:03:03');
If I do
select max(event_dt) from (
select usr,event_dt from mytable where usr= 'A') as foo
It is sort of what I need but it only returns the event_dt
"2009-01-02 11:10:22"
Where I want the usr as well sa event_dt from that row. How do I do it?
I would simply go for...
SELECT usr, event_dt FROM mytable ORDER BY event_dt DESC LIMIT 1