개발자
[ORACLE 4일차] 숫자함수(mod(나머지)/round(반올림)/trunc(절삭)/power(거듭제곱)/sign/ceil/floor/ascii/chr(A,65))/ 변환함수(to_char/case when then else end/decode/view/ 본문
[ORACLE 4일차] 숫자함수(mod(나머지)/round(반올림)/trunc(절삭)/power(거듭제곱)/sign/ceil/floor/ascii/chr(A,65))/ 변환함수(to_char/case when then else end/decode/view/
GoGo개발 2022. 9. 1. 20:43숫자함수
2.1 mod : 나머지를 구해주는 것
1
2
3
4
|
select 5/2, mod(5,2), trunc(5/2)
from dual;
-- 2.5 1 2
|
cs |
2.2 round : 반올림을 해주는 것.
1
2
3
4
5
6
7
8
|
select 94.547
, round(94.547) -- 95
, round(94.547, 0) -- 95 0 은 정수 1자리까지만 나타내어준다.
, round(94.547, 1) -- 94.5 1 은 소수 첫째자리까지만 나타내어준다.
, round(94.547, 2) -- 94.55 2 은 소수 둘째자리까지만 나타내어준다.
, round(94.547, -1) -- 90 -1 은 정수 10자리까지만 나타내어준다.
, round(94.547, -2) -- 100 -2 은 정수 100자리까지만 나타내어준다.
from dual;
|
cs |
2.3 trunc : 절삭을 해주는 것.
1
2
3
4
5
6
7
8
|
select 94.547
, trunc(94.547) -- 94
, trunc(94.547, 0) -- 94 0 은 정수 1자리까지만 나타내어준다.
, trunc(94.547, 1) -- 94.5 1 은 소수 첫째자리까지만 나타내어준다.
, trunc(94.547, 2) -- 94.54 2 은 소수 둘째자리까지만 나타내어준다.
, trunc(94.547, -1) -- 90 -1 은 정수 10자리까지만 나타내어준다.
, trunc(94.547, -2) -- 0 -2 은 정수 100자리까지만 나타내어준다.
from dual;
|
cs |
<예제>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
select hakbun AS 학번
, name AS 성명
, kor AS 국어
, eng AS 영어
, math AS 수학
, (kor+eng+math) AS 총점
, round( (kor+eng+math)/3, 1) AS 평균
, case
when round( (kor+eng+math)/3, 1) >= 90 then 'A'
when round( (kor+eng+math)/3, 1) >= 80 then 'B'
when round( (kor+eng+math)/3, 1) >= 70 then 'C'
when round( (kor+eng+math)/3, 1) >= 60 then 'D'
else 'F'
end AS 학점1
, case trunc( round( (kor+eng+math)/3, 1), -1)
when 100 then 'A'
when 90 then 'A'
when 80 then 'B'
when 70 then 'C'
when 60 then 'D'
else 'F'
end AS 학점2
, decode( trunc( ),round( (kor+eng+math)/3, 1 -1), 100, 'A'
, 90, 'A'
, 80, 'B'
, 70, 'C'
, 60, 'D'
, 'F') AS 학점3
from tbl_sungjuk;
|
cs |
2.4 power : 거듭제곱
1
2
3
|
select 2*2*2*2*2, power(2,5) -- 2의 5승
from dual;
|
cs |
-- 2.5 sqrt : 제곱근
select sqrt(16), sqrt(2), sqrt(3)
from dual;
-- 2.6 sin, cos, tan, asin, acos, atan
select sin(90), cos(90), tan(90), asin(0.3), acos(0.3), atan(0.3)
from dual;
-- 2.7 log
select log(10, 100)
from dual;
2.8 sign ==> 결과값이 양수이라면 1, 결과값이 0 이라면 0, 결과값이 음수이라면 -1
1
2
|
select sign(5-2), sign(5-5), sign(2-5) --1 , 0 -1
from dual;
|
cs |
2.9 ceil(실수) ==> 입력되어진 실수 보다 큰 최소의 정수를 나타내어준다.
ceil(정수) ==> 입력되어진 정수를 그대로 나타내어준다.
1
2
|
select ceil(10.1), ceil(10), ceil(-10.1), ceil(-10) --11 , 10, -10, -10
from dual;
|
cs |
2.10 floor(실수) ==> 입력되어진 실수 보다 작은 최대의 정수를 나타내어준다.
floor(정수) ==> 입력되어진 정수를 그대로 나타내어준다.
1
2
|
select floor(10.1), floor(10), floor(-10.1), floor(-10)
from dual;
|
cs |
2.11 ascii , chr
1
2
3
4
5
6
7
|
select ascii('A'), ascii('a'), ascii('0'), ascii(' ')
from dual;
-- 65 97 48 32
select chr(65), chr(97), chr(48), chr(32)
from dual;
-- A a 0 ' '
|
cs |
날짜 함수
날짜1 + 숫자 = 날짜2 ==> 날짜1 에서 숫자(일수)만큼 더한 값이 날짜2 가 된다.
날짜1 - 숫자 = 날짜2 ==> 날짜1 에서 숫자(일수)만큼 뺀 값이 날짜2 가 된다.
!!!!!!!! 여기서 중요한 것은 숫자의 단위가 일수 이다. !!!!!!!!!!
1
2
3
4
|
select sysdate - 1 , to_char(sysdate - 1, 'yyyy-mm-dd hh24:mi:ss') AS 어제시각 --22/08/31 , 2022-08-31 20:31:17
, sysdate , to_char(sysdate , 'yyyy-mm-dd hh24:mi:ss') AS 현재시각 -- 22/09/01 , 2022-09-01 20:31:17
, sysdate + 1 , to_char(sysdate + 1, 'yyyy-mm-dd hh24:mi:ss') AS 내일시각 --22/09/02 , 2022-09-02 20:31:17
from dual;
|
cs |
단위환산
1 kg = 1000 g
1 g = 1/1000 kg
1 일 = 24 시간
1 시간 = 1/24 일
1 시간 = 60 분
1 분 = 1/60 시간
1 분 = 60 초
1 초 = 1/60 분
<응용>
1
2
3
4
5
|
--- *** [퀴즈] 현재시각으로 부터 1일 2시간 3분 4초 뒤를 나타내세요 *** ----
select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') AS 현재시각
, to_char(sysdate + 1 + 2/24 + 3/(24*60) + 4/(24*60*60), 'yyyy-mm-dd hh24:mi:ss') AS "1일 2시간 3분 4초 뒤"
from dual;
-- 2022-07-01 16:42:52 2022-07-02 18:45:56
|
cs |
3.1 to_yminterval , to_dsinterval
to_yminterval 은 년 과 월을 나타내어 연산자가 + 이면 날짜에서 더해주는 것이고,
to_dsinterval 은 일 시간 분 초를 나타내어 연산자가 + 이면 날짜에서 더해주는 것이다.
연산자가 - 를 쓰면 날짜를 빼주는 것이다.
1
2
3
4
5
6
7
8
9
|
-- 현재일로 부터 1년 2개월 3일 4시간 5분 6초 뒤(미래)를 나타내세요...
select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') AS 현재시각
, to_char( sysdate + to_yminterval('01-02') + to_dsinterval('003 04:05:06'), 'yyyy-mm-dd hh24:mi:ss') AS "1년 2개월 3일 4시간 5분 6초 뒤"
from dual;
-- 현재일로 부터 1년 2개월 3일 4시간 5분 6초 전(과거)를 나타내세요...
select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') AS 현재시각
, to_char( sysdate - to_yminterval('01-02') - to_dsinterval('003 04:05:06'), 'yyyy-mm-dd hh24:mi:ss') AS "1년 2개월 3일 4시간 5분 6초 전"
from dual;
|
cs |
3.2 add_months(날짜,숫자)
숫자가 양수이면 날짜에서 숫자 개월수 만큼 더해준 날짜를 나타내는 것이고,
숫자가 음수이면 날짜에서 숫자 개월수 만큼 뺀 날짜를 나타내는 것이다.
여기서 숫자의 단위는 개월수 이다.
1
2
3
4
|
select to_char(add_months(sysdate, -2), 'yyyy-mm-dd hh24:mi:ss') AS "2개월전시각" --2022-07-01 20:38:46
,to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') AS 현재시각 --2022-09-01 20:38:46
,to_char(add_months(sysdate, 2), 'yyyy-mm-dd hh24:mi:ss') AS "2개월후시각" -- 2022-11-01 20:38:46
from dual;
|
cs |
-- 3.3 months_between(날짜1, 날짜2)
날짜1 에서 날짜2 를 뺀 값으로 그 결과는 숫자가 나오는데 결과물 숫자의 단위는 개월수 이다.
즉, 두 날짜의 개월차이를 구할 때 사용한다.
1
2
|
select months_between(add_months(sysdate,3), sysdate) --3
from dual;
|
cs |
변환 함수
to_char(날짜, '형태') ==> 날짜를 '형태' 모양으로 문자형태로 변환시켜주는 것이다. --
to_char(숫자, '형태') ==> 숫자를 '형태' 모양으로 문자형태로 변환시켜주는 것이다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
--- 날짜를 문자형태로 변환하기 ---
select to_char(sysdate, 'yyyy') AS 년도
, to_char(sysdate, 'mm') AS 월
, to_char(sysdate, 'dd') AS 일
, to_char(sysdate, 'hh24') AS "24시간"
, to_char(sysdate, 'am hh') AS "12시간"
, to_char(sysdate, 'pm hh') AS "12시간"
, to_char(sysdate, 'mi') AS 분
, to_char(sysdate, 'ss') AS 초
, to_char(sysdate, 'q') AS 분기 -- 1월~3월 => 1, 4월~6월 => 2, 7월~9월 => 3, 10월~12월 => 4
, to_char(sysdate, 'day') AS 요일명 -- 월요일(Windows) , Monday(Linux)
, to_char(sysdate, 'dy') AS 줄인요일명 -- 월(Windows) , Mon(Linux)
from dual;
select to_char(sysdate, 'd') -- 1(일요일) 2(월요일) 3(화요일) 4(수요일) 5(목요일) 6(금요일) 7(토요일)
from dual;
|
cs |
<예시>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
select case to_char(sysdate, 'd')
when '1' then '일'
when '2' then '월'
when '3' then '화'
when '4' then '수'
when '5' then '목'
when '6' then '금'
when '7' then '토'
end AS 오늘의요일명1
, decode(to_char(sysdate, 'd'), '1', '일'
, '2', '월'
, '3', '화'
, '4', '수'
, '5', '목'
, '6', '금'
, '7', '토')
AS 오늘의요일명2
from dual;
|
cs |
<예시2>
1
2
3
4
5
6
7
|
-- 숫자를 문자형태로 변환하기 --
select 1234567890
, to_char(1234567890, '9,999,999,999')
, to_char(1234567890, '$9,999,999,999')
, to_char(1234567890, 'L9,999,999,999') -- L 은 그 나라의 화폐기호 이다.
from dual;
-- 1234567890 1,234,567,890 $1,234,567,890 ₩1,234,567,890
|
cs |
view