I am not used to with Oracle
, same thing can be done in MySQL
. My task is that i have to save date in Oracle 19c, but no time. My Application language is J2EE. After doing some search found some solutions but my requirement is not being filled yet.
Solution 1 : Insert a Datetime Value Using the TO_DATE Function
CREATE TABLE abe_student (
first_name VARCHAR2(50),
last_name VARCHAR2(50),
date_of_birth DATE
);
INSERT INTO abe_student (first_name, last_name, date_of_birth)
VALUES ('John', 'Doe', TO_DATE('2016/01/16', 'yyyy/mm/dd'));
INSERT INTO abe_student (first_name, last_name, date_of_birth)
VALUES ('John', 'Doe', TO_DATE('16/01/2016', 'dd/mm/yyyy'));
This solution was for 10g. I have tried this one but it is inserting time with all zeros. Both queries are giving the same result.
solution two (10g): trunc()
remove all times from your DATE column (update yourtable set yourdatecolumn = trunc(yourdatecolumn))
ensure that all future dates contain no time part by placing a check constraint on the column by using check (yourdatecolumn = trunc(yourdatecolumn))
adjust all your INSERT and UPDATE statements or -if you're lucky- adjust your API, to only insert TRUNCed dates.
As per the instructions the query should be like :
update ABE_STUDENT set ABE_STUDENT.DATE_OF_BIRTH = trunc(DATE_OF_BIRTH);
After following the step 1, i have checked the DB, but no change.
TRUNC()
is not working but TO_CHAR()
is wokring fine.
SELECT TRUNC(DATE_OF_BIRTH), to_char(DATE_OF_BIRTH,'MM/DD/YYYY') FROM ABE_STUDENT
Lets introduce the use case why I need the data that format. The concept will be used for a financial service transactions analysis, where I need just the date. In complex query time, using another method to format the date, will be a performance issue.
update
after using : TRUNC(DATE_OF_BIRTH, 'DAY')
SELECT TRUNC(DATE_OF_BIRTH, 'DAY'), to_char(DATE_OF_BIRTH,'MM/DD/YYYY') FROM ABE_STUDENT