Oracle

210831_to char

요옫 2021. 8. 31. 12:37

to_char

to_char : date형, number형을 varchar2형으로 변환

to_date : char,varchar2형을 date타입으로 변환

 

 

(예제)
--날짜에서 년도만 추출
select to_char(sysdate,'year') from dual;  --영문으로 출력
select to_char(sysdate,'yyyy') from dual;  --2021

--날짜에서 월만 추출
select to_char(sysdate,'month') from dual;  --8월
select to_char(sysdate,'mm') from dual;--08

--날짜와 시간출력
alter session set time_zone='Asia/seoul';  --미국 시간으로 뜰 경우 해주기
select to_char(sysdate,'yyyy-mm-dd hh24-mi-ss') from dual;

--오라클 클라우드 같은 경우 sysdate가 미국현지 시간 얻어올 경우
select to_char(current_date,'yyyy-mm-dd hh-mi-ss') from dual;
select to_char(current_timestamp,'yyyy-mm-dd hh-mi-ss') from dual;

-- to_char는 숫자에도 적용가능
select to_char(123458953,'999,999,999') from dual;  --천단위 구분기호
select to_char(34.8,'9999.00')from dual;  --9는 값이 없으면 공백으로
select to_char(34.8,'00999.00')from dual;  --0은 값이 없으면 0으로 채워서 출력

--person 테이블에서 입사월이 7월인 사람 출력
select * from person where to_char(ipsaday,'mm')='07';

--person 테이블에서 입사월이 1997년인 사람 출력
select * from person where to_char(ipsaday,'yyyy')='1997';

'Oracle' 카테고리의 다른 글

210831_update,delete  (0) 2021.08.31
210831_서브쿼리  (0) 2021.08.31
210831_그룹함수  (0) 2021.08.31
210831_sequence  (0) 2021.08.31
210830_  (0) 2021.08.30