Usage of Oracle syntax to_char, to_date and add_months

Table of contents

1. Convert string to time format

2. Convert the string to a time format and then convert it to a string according to the specified format

3. Search by month

4. Usage of ADD_MONTHS

5. The day before

6. Oracle modifies the attributes of table fields


1. Convert string to time format

select to_date ('2022-12-31','yyyy-MM-dd') --2022-12-31 00:00:00
select to_date ('20221231','yyyy-MM-dd')   --2022-12-31 00:00:00
select to_date ('20221231')                --2022-12-31 00:00:00
select to_date ('20221231','yyyy/MM/dd')   --2022-12-31 00:00:00
select to_date ('20221231','yyyy,MM,dd')   --2022-12-31 00:00:00

2. Convert the string to a time format and then convert it to a string according to the specified format

select to_char(to_date('20211110'),'yyyy-mm-dd'); --2021-11-10  先将字符串转换成时间格式,然后再使用to_char转换成指定改革是的字符串
select to_char(to_date('20211110'),'yyyymmdd');   --20211110
select to_char(to_date('20211110'),'yyyy-MM-dd HH24:mi:ss');   --2021-11-10 00:00:00
select to_char(to_date('20211110'),'yyyy-MM-dd HH:mi:ss');   --2021-11-10 12:00:00
select to_char(to_date('20211110'),'yyyymmdd')  where to_char(to_date('20211110'),'yyyymmdd')  <> '20211110'  --<>表示不等于

3. Search by month

select * from fdm.f_tx_trans_info_mid where to_char(trans_date ,'mm') = to_char(to_date('20201231') ,'mm')                         --交易信息表,可以用来查询本月所有的交易(还可以用来查询本月过生的人等等,年月日以此类推)

4. Usage of ADD_MONTHS

select yinyrq,TO_CHAR(ADD_MONTHS(TO_DATE( yinyrq ,'YYYYMMDD'),-12),'YYYYMMDD') from fdm.f_tx_l_hx_dp_hxjyls_mid ftlhdhm            --核心交易流水表,时间格式是字符串 20201231  20191231

5. The day before

select yinyrq,TO_CHAR((TO_DATE( yinyrq ,'YYYYMMDD')-1),'YYYYMMDD') from fdm.f_tx_l_hx_dp_hxjyls_mid ftlhdhm                        --前一天 20201231  20201230
SELECT TO_CHAR((TO_DATE('20201231','YYYYMMDD') - INTERVAL '6 MONTH'),'YYYYMMDD') as time1     --20200630
SELECT TO_CHAR(ADD_MONTHS(TO_DATE('20201231','YYYYMMDD'),-2),'YYYYMMDD') as tim ;    --20201031

6. Oracle modifies the attributes of table fields

alter table MDM.M_RPT_FLOW_LOAN_SETL_DATA MODIFY(CR_CUST_NM CHARACTER VARYING(200));

7. Get the day, month, and year of the date

select date_part('day',to_date('20201231','YYYYMMDD')) --31
select date_part('month',to_date('20201231','YYYYMMDD')) --12
select date_part('year',to_date('20201231','YYYYMMDD')) --2020

The content is for reference only.

Guess you like

Origin blog.csdn.net/m0_64210833/article/details/128598865