0

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.

DB insert result

solution two (10g): trunc()

  1. remove all times from your DATE column (update yourtable set yourdatecolumn = trunc(yourdatecolumn))

  2. ensure that all future dates contain no time part by placing a check constraint on the column by using check (yourdatecolumn = trunc(yourdatecolumn))

  3. 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

result after truc and to_char

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

enter image description here

Black Swan
  • 813
  • 13
  • 35
  • You must specify the unit: `TRUNC(DATE_OF_BIRTH, 'DAY')` – Akina Apr 29 '21 at 09:36
  • 1
    Oracle dates [always have a time](https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Data-Types.html#GUID-5405B652-C30E-4F4F-9D33-9A4CB2110F1B) component. You can't have a date without a time. Setting it to midnight, as you are, is the closest you can get. (And `trunc()` defaults to DD, which is equivalent to DAY, so doesn't make a difference if you specify that; both just set time to midnight, which makes no difference here anyway). And dates don't have a format until you display them. It isn't clear why this is a problem for you? – Alex Poole Apr 29 '21 at 09:39
  • @Alex Poole but search is not working with the midnight time. SELECT * FROM ABE_STUDENT WHERE DATE_OF_BIRTH = "2016-01-10 00:00:00" – Black Swan Apr 29 '21 at 09:59
  • 1
    @BlackSwan - double quotes are for identifiers not strings; but compare dates with dates: `where date_of_bith = date '2016-01-10'`. You should only format dates as strings for final display (or let the client/application do it); leave them as dates for comparisons and calculations. – Alex Poole Apr 29 '21 at 10:00
  • @BlackSwan Don't use double quotes as those are for identifiers; you want single quotes for literals. Don't use a string for a date; use a date literal `DATE '2016-01-10'` or a timestamp literal `TIMESTAMP '2016-01-10 00:00:00'`. Finally, are you sure that your values are actually at midnight and not some other time of the day? `WHERE date_of_birth >= DATE '2016-01-10' AND date_of_birth < DATE '2016-01-11'` (or `WHERE TRUNC( date_of_birth ) = DATE '2016-01-10'` however, that will not use an index on the column and would require a function-based index). – MT0 Apr 29 '21 at 10:03

2 Answers2

1

You cannot.

A DATE in Oracle is a binary data-type which ALWAYS contains 7-bytes representing century, year-of-century, month, day, hour, minute and second.

If you want to display a DATE data type without the time component then convert it to a formatted string (either using the TO_CHAR function in the query or in the 3rd-party application you are using to access the database).

If you want to store the DATE without regards to the time component then insert the values so they always have the time component at midnight; you can use a CHECK constraint to enforce this.

MT0
  • 143,790
  • 11
  • 59
  • 117
  • Or have the application do the formatting, and leave the data in the native data-type. This is akin to complaining that a float has too many digits. – MatBailie Apr 29 '21 at 09:46
0

You can use a generated column to generate the string form. I would use:

alter table abe_student add column dob_string varchar2(255)
    generated always as (to_char(date_of_birth, 'YYYY-MM-DD'));

(I have a strong preference for standard date formats.)

Then you can query the table using the string column. Voila! It will appear exactly as you want it.

You can ensure that the column has no time component using a check constraint:

alter table abe_student add constraint chk_abe_student_dob
    check (date_of_birth = trunc(date_of_birth));

Or you could configure your GUI interface to display only the date for a date column.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786