Inserting timestamp in oracle timestamp column - oracle

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.

Related

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

Date format in where clause-Oracle

I have one table where date column is having data in below format:
"7/25/2014 12:14:27 AM'. I need to fetch this date by putting in the where clause. can anyone suggest how can i do this?
Dates (stored in tables) are represented by 7 bytes - they do not have any format associated with them. If they are formatted as a string then that is the client program which you are using to access the database applying its own formatting to the date (which you can usually set via the preferences in that program).
If the "date" is stored with a format then you are not storing it as a date but storing it as a string (i.e. VARCHAR2) format.
Query - if the date is stored as a date:
SELECT *
FROM table_name
WHERE date_column = TO_DATE( '7/25/2014 12:14:27 AM', 'MM/DD/YYYY HH12:MI:SS AM' )
or, using ANSI/ISO literals:
SELECT *
FROM table_name
WHERE date_column = TIMESTAMP '2014-07-25 00:14:27'
or, if you want values which are just for a given day then:
SELECT *
FROM table_name
WHERE date_column >= DATE '2016-05-12'
AND date_column < DATE '2016-05-13'
(This will allow you to use any indexes on the date_column column.)
or, only passing a single day value (represented by the bind variable :date_value):
SELECT *
FROM table_name
WHERE date_column >= :date_value
AND date_column < :date_value + INTERVAL '1' DAY
Query - if the "date" is stored as a string:
SELECT *
FROM table_name
WHERE TO_DATE( date_as_a_string_column, 'MM/DD/YYYY HH12:MI:SS AM' )
= TIMESTAMP '2014-07-25 00:14:27'
or, just simply:
SELECT *
FROM table_name
WHERE date_as_a_string_column = '7/25/2014 12:14:27 AM'
Most likely the date format you see is the format your SQL tool (SQLplus, SQL Developer, Toad etc.) uses. Proper date columns don't have an associated date format.
To write a reliable query for selecting by date, explicitly specify the date format in your query (otherwise Oracle we use the date format from your current session):
SELECT * FROM T1
WHERE DATE_COL > TO_DATE('7/25/2014 12:14:27 AM', 'MM/DD/YYYY HH:MI:SS AM');
Any other date format will also work:
SELECT * FROM T1
WHERE DATE_COL > TO_DATE('2014-07-25 12:14:27', 'YYYY-MM-DD HH24:MI:SS');

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.

how to convert a string date to date format in oracle10g

My date value is stored as varchar2 and the value is 15/August/2009,4:30 PM, how to convert this to a proper date format like DD-MM-YYYY.
You can convert a string to a DATE using the TO_DATE function, then reformat the date as another string using TO_CHAR, i.e.:
SELECT TO_CHAR(
TO_DATE('15/August/2009,4:30 PM'
,'DD/Month/YYYY,HH:MI AM')
,'DD-MM-YYYY')
FROM DUAL;
15-08-2009
For example, if your table name is MYTABLE and the varchar2 column is MYDATESTRING:
SELECT TO_CHAR(
TO_DATE(MYDATESTRING
,'DD/Month/YYYY,HH:MI AM')
,'DD-MM-YYYY')
FROM MYTABLE;
You need to use the TO_DATE function.
SELECT TO_DATE('01/01/2004', 'MM/DD/YYYY') FROM DUAL;

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