简体   繁体   中英

Extract month and year from date in oracle

what is the query for extract month and year from full date. my data is like this: 1/29/2008 I tried this query:

select ID_NO, CHECKED_DATE, to_date(TO_CHAR(CHECKED_DATE, 'MON-YYYY'), 'MON-YYYY') AS A 
from Doctor_Checkup;

but It will give output: 1/1/2008

Expected output: 1-2008

If the field is already a date column, you can simply cast it to the format you want:

select ID_NO,CHECKED_DATE,ltrim(TO_CHAR(CHECKED_DATE,'mm-yyyy'),'0') AS A from Doctor_Checkup;

If it is a text column, you will need to cast to a date with format first:

select ID_NO,CHECKED_DATE,ltrim(TO_CHAR(TO_DATE(CHECKED_DATE,'dd/mm/yyyy'),'mm-yyyy'),'0') AS A from Doctor_Checkup;

A date does not have a format - it is stored internally to the database as 7-bytes (representing year, month, day, hour, minute and second) and it is not until whatever user interface you are using (ie SQL/Plus, SQL Developer, Java, etc) tries to display it to you, the user, and converts it into something you would find meaningful (usually a string) that the date has a format.

One thing to note is that a date always has the year, month, day, hour, minute and second components. Doing:

to_date(TO_CHAR(CHECKED_DATE, 'MON-YYYY'), 'MON-YYYY')

Is effectively the same as doing:

TRUNC( Checked_Date, 'MM' )

and will still have a day, hour, minute and second component but will have been truncated to midnight of the first day of the month. The user interface may just be have its preferences set to not display the time component (but the date will still have one).

What you want to do is convert the date to a formatted string:

select ID_NO,
       CHECKED_DATE,
       TRIM( LEADING '0' FROM TO_CHAR( CHECKED_DATE, 'MM-YYYY') ) AS A 
from   Doctor_Checkup;

or

select ID_NO,
       CHECKED_DATE,
       EXTRACT( MONTH FROM CHECKED_DATE )
         || '-' || EXTRACT( YEAR FROM CHECKED_DATE ) AS A 
from   Doctor_Checkup;

要获得1-2008格式,请使用以下格式并修剪前导零:

select ID_NO,CHECKED_DATE,ltrim(TO_CHAR(CHECKED_DATE,'MM-YYYY'),'0') AS A from Doctor_Checkup; 

You want a string representing month and year in the format [M]M-YYYY. Oracle's TO_CHAR only supports MM-YYYY, though, so you'd have to get rid of a leading zero if any.

Two solutions:

trim(leading '0' from to_char(checked_date, 'mm-yyyy'))

or

extract(month from checked_date) || '-' || extract(year from checked_date) from dual

SELECT ID_NO, CHECKED_DATE FROM DOCTOR_CHECKUP EXTRACT(MONTH FROM CHECKED_DATE) IN (6) AND EXTRACT(YEAR FROM CHECKED_DATE) IN (2019);

6 MEANS THE MONTH AND 2019 IS THE YEAR

LTRIM(TO_CHAR(TO_DATE(<date_field>,'YYYYMMDD'),'YYYY-MM'),'09'))

In order to select Month and a Year from a String 'AB0906DA' can do the following in PL/SQL:

DECLARE
     lv_promo_code VARCHAR2(10) := 'AB0906DA';
     lv_promo_num VARCHAR2(5);
     lv_promo_month NUMBER(4);
     lv_promo_year NUMBER(4);
BEGIN
    lv_promo_num := REGEXP_SUBSTR(lv_promo_code, '(\d)(\d)(\d)(\d)');

    lv_promo_month := EXTRACT(month from to_date(lv_promo_num, 'MMYY'));
    DBMS_OUTPUT.PUT_LINE(lv_promo_month);
    lv_promo_year := EXTRACT(year from to_date(lv_promo_num, 'MMYY'));
    DBMS_OUTPUT.PUT_LINE(lv_promo_year);
END;
/

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