오라클

오라클(SQL) 함수2(형 변환 함수)

웹개발자준비 2018. 9. 3. 18:51

1. to_char() : 숫자 -> 문자

2. to_char() : 날짜 -> 문자

3. to_number() : 문자 -> 숫자

4. to_date() : 문자 -> 날짜


-----------------------------------------------------------------------------------------------------------

1. to_char() : 숫자 -> 문자

- char to_char(컬럼명,형식 문자열)

- 형식 문자열 구성 요소

a.9

b.0 - 숫자 1자리를 문자 1개로 바꾸는 역할(모자란 자리는 '0'으로 채운다.

c. $ : 달러 표시

d. L : 지역별 통화 표시(Locale)

e. . : 소수점 표시

f. , : 천단위 표시


ex)

-- 실인자인 숫자보다 형문자열의 자리수가 더 크면 형변환 불가(####)

select to_char(1000,'999') from dual; --1000 -> '1000'

select to_char(1000,'000') from dual; --1000 -> '1000'


select 

    weight, weight || 'kg' ,

    to_char(weight,'999') || 'kg',

    to_char(weight,'000') || 'kg' ,

    lpad(weight,3,'0') || 'kg'

from tblname;


--주의점 : trim() 처리를 해서 쓸모없는 공백을 제거 후 사용하는 것을 권장

select 

    '@' ||  to_char(-100,'999'),

    '@' ||  to_char(100,'999')

from dual;


select length(to_char(1,'999')), '@' || ltrim(to_char(1,'999')) from dual;


select to_char(100,'$999') from dual; --달러가 붙는다.

select to_char(100,'999원') from dual;

select to_char(100,'L999') from dual; --지역별로 바뀐다.

select to_char(100,'999') || '원' from dual;


select to_char(123.456,'999.999') from dual; --좌측정렬(문자) / 우측정렬(숫자)


select to_char(123.456,'9999.99') from dual; --반올림 + 문자열 변환

select to_char(1000000,'9,999,999') from dual; -- 세자리마다 자기가 찍어야댐.


2. to_char() : 날짜 -> 문자

- char to_char(컬럼명, '형식 문자열')

***형식 문자열 구성 요소

-yyyy

-yy

-month

-mon

-mm

-day

-dy

-ddd, dd, d

-hh(hh12),hh24

-mi

-ss

-am(pm)


ex)

select to_char(sysdate,'yyyy') from dual;       -- 2018년 (4자리)(***************************)

select to_char(sysdate,'yy') from dual;          -- 18년(2자리)X

select to_char(sysdate,'month') from dual;    --8월,로케일(풀네임)

select to_char(sysdate,'mon') from dual;       --8월,로케일(약어)

select to_char(sysdate,'mm') from dual;        --08, 2자리 숫자(***********************************)

select to_char(sysdate,'day') from dual;        --요일, 로케일(풀네임)

select to_char(sysdate,'dy') from dual;         --요일,로케일(약어)

select to_char(sysdate,'ddd') from dual;        --236, 올해 들어 며칠째인지?

select to_char(sysdate,'dd') from dual;         --24, 이번달 들어 며칠째인지?

select to_char(sysdate,'d') from dual;              --6. 이번주 들어 며칠째인지? > 요일(1~7)

select to_char(sysdate,'hh') from dual;         --11.시간(12시간)

select to_char(sysdate,'hh24') from dual;       --11.시간(24시간)

select to_char(sysdate,'mi') from dual;         --02.분

select to_char(sysdate,'ss') from dual;            --44.초

select to_char(sysdate,'am') from dual;         --오전/오후

select to_char(sysdate,'pm') from dual;         --오전.오후


3. to_number() : 문자 -> 숫자

- number to_number(문자열)

- 자바 : Integer.parseInt(문자열)


ex)

select to_number('123') *2 from dual;

select '123' *2 from dual;


4. to_date() : 문자 -> 날짜(****)

-date to_date(컬럼명, '형식 문자열')

- 형식 문자열이 2번과 동일


-SQL은 날짜시간 데이터의 리터럴은 문맥에 따라 문자열 OR 날짜 시간 중 선택


ex)

select to_date('2018-08-24','yyyy-mm-dd'),sysdate from dual; --*****************

select to_date('2018-08-24 11:37:40','yyyy-mm-dd hh24:mi:ss'),sysdate from dual; --****************