简体   繁体   中英

Extract month number from a date and update the same table -Oracle

I have table hire_table containing 2 columns id , hire_date . the hire_date is of the format dd-mm-yyyy format . Is there a way to extract the month number from the date and simultaneously update the hire_table ?

i know a solution like as shown below.

1) create a separate table to extract id and month number.

create table monthtable as(
select id , EXTRACT(month FROM hire_date) as monthno
from hire_table);

2)using left join combine the 2 tables.

create table new_hiretable as(
select hiretable.* , monthtable.monthno
from hiretable left join monthtable
on hiretable.id=monthtable.id);

My question is , is there a better way without creating new tables and updating the month number column in the original table ?

It usually isn't a good idea to store redundant data - the month number can be extracted from the full date at run time, so storing it separately just gives you duplication and the potential for mistakes, if one column is updated and the other is not; and you have to set the extra column value, eg via a trigger.

I'm also not sure if you really want the month number (with no reference to the year) or really want the hire month without the specific day, so I'll generate both.

You can get the value(s) from a query like:

select id, hire_date,
  extract(month from hire_date) as month_no,
  trunc(hire_date, 'MM') as hire_month
from hire_table;

If you want to hide that you can then create a view based on that query:

create view hire_view as
select id, hire_date,
  extract(month from hire_date) as month_no,
  trunc(hire_date, 'MM') as hire_month
from hire_table;

and query that instead:

select * from hire_view;

Assuming you're on 11g or higher you can also modify your table to have a virtual column instead:

alter table hire_table add (
  month_no number generated always as (extract(month from hire_date)) virtual,
  hire_month date generated always as (trunc(hire_date, 'MM')) virtual
);

When you query the table you'll see the generated values, with no additional maintenance required:

insert into hire_table (id, hire_date) values (1, date '2015-07-04');

select * from hire_table;

        ID HIRE_DATE    MONTH_NO HIRE_MONTH
---------- ---------- ---------- ----------
         1 2015-07-04          7 2015-07-01

Your second ctas query is similar to below query:

SELECT hiretable.*, 
       EXTRACT(MONTH FROM hire_date) AS monthno
FROM   hiretable;

Is it really necessary to create a new table to update hire_date in your table. If not you can use the below query to update the column.
However if your column is of date datatype, you need to convert month into DATE as here the EXTRACT function will return a number which cannot be used to update a column with date.

UPDATE hire_table 
SET    hire_date = to_date(EXTRACT(MONTH FROM hire_date),'mm') ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM