简体   繁体   English

从oracle中的日期中提取月份和年份

[英]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:我的数据是这样的:1/29/2008 我试过这个查询:

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但它会给出输出:1/1/2008

Expected output: 1-2008预期产出: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. 日期没有格式 - 它以7字节(代表年,月,日,小时,分钟和秒)的内部存储在数据库中 ,直到您使用的是任何用户界面(即SQL / Plus, SQL Developer,Java等尝试将它显示给您,即用户,并将其转换为您认为有效的(通常是字符串)日期具有格式的内容。

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. 您想要一个表示月份和年份的字符串,格式为[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. 但是Oracle的TO_CHAR只支持MM-YYYY,所以你必须摆脱前导零(如果有的话)。

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); 选择ID_NO,CHECKED_DATE来自DOCTOR_CHECKUP提取物(从CHECKED_DATE开始的月份)(6)和提取物(从CHECKED_DATE开始)(2019年);

6 MEANS THE MONTH AND 2019 IS THE YEAR 6意味着这个月和2019年是一年

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:为了从字符串 'AB0906DA' 中选择月份和年份,可以在 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;
/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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