unable to insert into column (Oracle) - oracle

I am trying to insert into my date column of a table where my id column already has a value.I tried to use this query but it gave me error ORA00917 (missing coma)
insert into EXAMPLE
(TIME_1)
values(
(TO_DATE('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'))
where ID=23);
Can anyone tell me where I did wrong?

Insert does not do updates.
update example
set time_1 = TO_DATE('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss')
where ID = 23;

Related

Combine Different Date and Time Column to make one Column with Date/Time format in Oracle SQL

I am working with database in which I have appointment table. With Two columns
ApptDate ApptTime
9/26/21 9:00 AM
9/25/20 1:00 PM
I want to drop the ApptTime column after making ApptDate Columns as ApptDateTime column.
I have tried concatnate but I can't figure out, how can I change datatype of AppDate column to DATETIME data type and update all the values simultaneously.
I tried following:
ALTER SESSION SET nls_date_format = 'DD-MON-YYYY hh24:mi'
UPDATE Appointment
SET ApptDate = ApptDate ||' ' ||ApptTime;
ALTER TABLE Appointment
MODIFY(
ApptDate DATE
);
But I got error that Data type can be changed of only Empty Table.
Kindly suggest.
Case 1 : If ApptDate column is of string type, then recreate and populate your table as
/*CREATE TABLE Appointment( ApptDate VARCHAR2(15), ApptTime VARCHAR2(15) );
INSERT INTO Appointment
SELECT '9/26/21','9:00 AM' FROM dual UNION ALL
SELECT '9/25/20','1:00 PM' FROM dual; -- already existing state */
CREATE TABLE Appointment2 AS
SELECT TO_DATE(ApptDate||' '||ApptTime,'MM/DD/RR HH:MI PM') AS ApptDate
FROM Appointment;
DROP TABLE Appointment;
RENAME Appointment2 TO Appointment
Case 2 : If data type of ApptDate column is date, then use the following code block
/*CREATE TABLE Appointment( ApptDate DATE, ApptTime VARCHAR2(15) );
INSERT INTO Appointment
SELECT date'2021-09-26','9:00 AM' FROM dual UNION ALL
SELECT date'2020-09-25','1:00 PM' FROM dual; -- already existing state */
CREATE TABLE Appointment2 AS
SELECT TO_DATE(ApptDate||' '||ApptTime,'RRRR-MM-DD HH:MI PM') AS ApptDate
FROM Appointment;
DROP TABLE Appointment;
RENAME Appointment2 TO Appointment
Demo

Oracle: Why do I have to use TO_DATE to pull my data?

When trying to filter on EXPIRE_DATE, it seems I have to use TO_DATE. Why do I have to use TO_DATE? The EXPIRE_DATE data type in the database is ALREADY set to date. Here is the code that works.
SELECT * FROM MY_TABLE
WHERE EXPIRE_DATE >= TO_DATE('2020/01/13','yyyy/mm/dd')
AND EXPIRE_DATE <= TO_DATE('2020/04/19','yyyy/mm/dd')
I tried to use BETWEEN without TO_DATE and just use my dates but I received an error.
To recap, even though the data type for this is ALREADY date, it seems I have to use TO_DATE to pull my data when I want to filter. Is there something I am missing? Here is my error when I try filter my data without using TO_DATE.
Apart from some good answers here, I would like to tell you that you do not need TO_DATE to pull the data from your table.
You need to_date or date literal to convert the normal string to date which can be compared to the column data in your table, As the date column must be compared with the date data type variable/constant.
To convert normal string to date, You can use the following:
TO_DATE('2020/01/13','yyyy/mm/dd')
DATE '2020-01-13'
I would not recommend using NLS_DATE_FORMAT just for creating the date.
You don't need to use TO_DATE, instead you can use a DATE literal:
SELECT *
FROM MY_TABLE
WHERE EXPIRE_DATE >= DATE '2020/01/13'
AND EXPIRE_DATE <= DATE '2020/04/19'
Or, if your NLS_DATE_FORMAT session parameter matches YYYY/MM/DD then you can insert the values as strings and rely on implicit string conversion (don't do this though):
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY/MM/DD';
SELECT *
FROM MY_TABLE
WHERE EXPIRE_DATE >= '2020/01/13'
AND EXPIRE_DATE <= '2020/04/19'
But it is not good practice to rely on the NLS_DATE_FORMAT as ANY user can change their own value at ANY time so your query can randomly fail when users start changing these values.
You can use BETWEEN:
Warning: it's better to use always TO_DATE function with proper format string and avoid implicit conversions that in some occasions produces strange behaviors in the results.
Warning: all columns of type DATE have always the hour-minute-second component.
if you forget this you may have fewer records in the result.
Example:
create table my_table
(id number,
expire_date date
);
Some data:
insert into my_table values ( 4,to_date('2011-06-17 10:07:18','yyyy-mm-dd hh24:mi:ss'));
insert into my_table values (12,to_date('2010-10-01 17:43:30','yyyy-mm-dd hh24:mi:ss'));
insert into my_table values (13,to_date('2011-07-30 08:38:34','yyyy-mm-dd hh24:mi:ss'));
insert into my_table values (21,to_date('2010-04-22 07:03:35','yyyy-mm-dd hh24:mi:ss'));
insert into my_table values (26,to_date('2011-03-26 02:07:57','yyyy-mm-dd hh24:mi:ss'));
insert into my_table values (35,to_date('2010-09-16 17:40:01','yyyy-mm-dd hh24:mi:ss'));
insert into my_table values (38,to_date('2011-11-05 17:27:45','yyyy-mm-dd hh24:mi:ss'));
insert into my_table values (44,to_date('2011-12-25 04:51:24','yyyy-mm-dd hh24:mi:ss'));
insert into my_table values (45,to_date('2011-11-05 03:08:51','yyyy-mm-dd hh24:mi:ss'));
insert into my_table values (54,to_date('2011-09-22 18:29:14','yyyy-mm-dd hh24:mi:ss'));
insert into my_table values (78,to_date('2010-03-12 20:23:21','yyyy-mm-dd hh24:mi:ss'));
insert into my_table values (79,to_date('2011-05-19 17:30:15','yyyy-mm-dd hh24:mi:ss'));
insert into my_table values (83,to_date('2011-11-15 10:04:58','yyyy-mm-dd hh24:mi:ss'));
insert into my_table values (96,to_date('2011-03-11 20:14:30','yyyy-mm-dd hh24:mi:ss'));
Set default date format to ISO-8601 international format:
alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS';
Query using implicit conversion:
SELECT a.*
fROM my_table a
where expire_date between '2010-01-01 00:00:00'
and '2010-12-31 23:59:59';
Answer:
ID EXPIRE_DATE
12 2010-10-01 17:43:30
21 2010-04-22 07:03:35
35 2010-09-16 17:40:01
78 2010-03-12 20:23:21
but using US date format month-day-year:
SELECT a.*
fROM my_table a
where expire_date between '01/01/2010 00:00:00'
and '12/31/2010 23:59:59';
You got error:
ORA-01861: literal does not match format string
Change session date format to US format:
alter session set nls_date_format='MM-DD-YYYY HH24:MI:SS';
you can write dates in US format:
SELECT a.*
fROM my_table a
where expire_date between '01/01/2010 00:00:00'
and '12/31/2010 23:59:59';
And the answer is:
ID EXPIRE_DATE
---------- -------------------
12 10-01-2010 17:43:30
21 04-22-2010 07:03:35
35 09-16-2010 17:40:01
78 03-12-2010 20:23:21

Oracle TIMESTAMP Comparison with TO_TIMESTAMP function

I am having a problem getting the result I expect from an Oracle query.
The TO_TIMESTAMP I am using appears to work fine:
SELECT TO_TIMESTAMP('11-16-2014 00:00:00', 'mm-dd-yyyy hh24:mi:ss') FROM DUAL
Returns
2014-11-16 00:00:00
My table AUDIT has a column CURRENT_TIMESTAMP of Oracle type TIMESTAMP(6). I don't know if it helps but this column also has these attributes:
DATATYPE=93
COLUMN_SIZE=11
DECIMAL_DIGITS=6
NUM_PREC_RADIX=10
CHAR_OCTET_LENGTH=11
Lets look at the table size:
SELECT count(*) FROM RPT.AUDIT
returns
623981
This table grows about 500 rows a day. So I would expect this query to return a number under 1000.
Instead I get the whole table:
SELECT count(*) FROM RPT.AUDIT WHERE CURRENT_TIMESTAMP > TO_TIMESTAMP('11-16-2014 00:00:00', 'mm-dd-yyyy hh24:mi:ss')
returns
623981
Thanks if you can help.
Because CURRENT_TIMESTAMP is the name of an Oracle function the database prefers to use the function instead of your column - and thus, since CURRENT_TIMESTAMP (the function) is always greater than a time in the past the predicate returns TRUE for every row in the table, and thus every row gets counted. If you put a table alias in your query and qualify the column name with the alias you should get what you expected:
SELECT count(*)
FROM RPT.AUDIT a
WHERE a.CURRENT_TIMESTAMP > TO_TIMESTAMP('11-16-2014 00:00:00', 'mm-dd-yyyy hh24:mi:ss')
Or you can just refer to the column as RPT.AUDIT.CURRENT_TIMESTAMP if you like.
Share and enjoy.

Inserting timestamp in oracle timestamp column

I have a table in which there is a column with datatype TIMESTAMP(0)
When I insert a date into this column using
INSERT INTO TEST_TIMESTAMP VALUES(SYSDATE)
it inserts a date in the following example format
12-SEP-12 10.31.19.000000000 AM
I want to know how the below timestamp formats can be inserted in the table
12-SEP-12 10.31.19 and 12-SEP-12 10.31.19 AM
I tried specifying some formats using TO_CHAR while inserting SYSDATE into the table, but it didn't work.
Please suggest.
when you store a TIMESTAMP it will always store the data at maximum precision (with fractional seconds).
I think what you want to do is supply a format to display the date when you retrieve it from the database.
You can do this like so:
select to_char(timestampColumnName,'DD-MON-YY HH24:MI:SS') "Date" from test_timestamp
or
select to_char(timestampColumnName,'DD-MON-YY HH:MI:SS AM') "Date" from test_timestamp
You can return it very easy like:
SELECT to_char(sysdate,'DD-MON-YY HH24:MI:SS') FROM DUAL;
In your case use:
INSERT INTO TEST_TIMESTAMP(column_name) VALUES(to_char(sysdate,'DD-MON-YY HH24:MI:SS'));
You were missing the extra ().
INSERT INTO TEST_TIMESTAMP
VALUES (TO_TIMESTAMP('12-SEP-12 10.31.19', 'DD-MON-YY HH.MI.SS'));
INSERT INTO TEST_TIMESTAMP
VALUES (TO_TIMESTAMP('12-SEP-12 10.31.19 AM', 'DD-MON-YY HH.MI.SS AM'));
TO_CHAR(SYSDATE, 'DD/MM/YYYY HH24:MI:SS')
This for colum type insert over mode to_char.

how to insert text to date field in oracle

how to insert (text) time: 09:44:02 and date:11/09/2007 to date filed in Oracle ?
i have field Tdate (date) and Ttime (date) in oracle
thanks in advance
Since a date is a point in time you should really only use one field:
insert into your_table (dt)
values (to_date ('11/09/2007 09:44:02', 'dd/mm/yyyy hh24:mi:ss'));
Since your model contains two columns, you could use:
insert into your_table (Tdate, Ttime)
values (to_date('11/09/2007', 'dd/mm/yyyy'),
to_date ('09:44:02', 'hh24:mi:ss'));
Note however that by default your Ttime column will contain date information which may be false/irrelevant since you can not store only the time component:
SQL> select to_char(tdate), to_char(ttime) from your_table;
TO_CHAR(TDATE) TO_CHAR(TTIME)
------------------- -------------------
11/09/2007 00:00:00 01/08/2011 09:44:02
INSERT INTO <table>
(date_column)
VALUES
(TO_DATE(tdate||' '||ttime, 'DD/MM/YYYY HH24:MI:SS'))
/
If it is in PL/SQL
DECLARE
v_date_field DATE;
BEGIN
v_date_field := TO_DATE(tdate||' '||ttime, 'DD/MM/YYYY HH24:MI:SS');
--
INSERT INTO <table>
(date_column)
VALUES
(v_date_field);
END;
/
Don't forget to commit afterwards :-)
insert into tablename(tdate)
values(to_date('11/09/2007 09:44:02','dd/mm/yyyy hh24:mi:ss'))

Resources