๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
DB - SQL

[SQL ์ž…๋ฌธ] 8. ๋ณ€ํ™˜ํ•จ์ˆ˜ + TO_CHAR

by ๊พธ๋ฆฌ_No9 2024. 1. 26.

DATABASE SQL ORACLE

 

[ํ•™์Šต ๋ชฉํ‘œ]
1. ๋‚ ์งœ ๋ฐ์ดํ„ฐ → ๋ฌธ์ž ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜, ๋ฌธ์žํ™” ํ‘œ๊ธฐ

  •  '์ž‘์€ ๋”ฐ์˜ดํ‘œ'๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ : ๋ฌธ์ž ( '00700' ๋ฌธ์ž , 00700 ์ˆซ์ž)  

2. To_char๋ฅผ ํ†ตํ•œ ์…€ ์„œ์‹ ๋ณ€๊ฒฝ 

3. ๋ฌธ์ œ : "12์›”์— ์ž…์‚ฌํ•œ ์‚ฌ๋žŒ"์˜ ์ด๋ฆ„๊ณผ ์ž…์‚ฌ์ผ ์ฐพ๊ธฐ (To_char & Extract ํ•จ์ˆ˜ ์‚ฌ์šฉ)

 

์•”์‹œ์  ๋ณ€ํ™˜ :
โ–ถ ์‹œ์Šคํ…œ์—์„œ ์ž๋™์ ์œผ๋กœ ๋ฌธ์ž๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜
โ–ถ select  '007'+ 007  from dual ;  ๋ฌธ์ž(๋ณ€ํ™˜ ์ˆซ์ž) +์ˆซ์ž 
    ๊ฒฐ๊ณผ : 14

 

 

1. To_char๋ฅผ ํ†ตํ•œ ๋ฌธ์ž  ํ‘œ๊ธฐ  (๋…„๋„ / ์›” / ์š”์ผ ์ง€์ • ํ‘œ๊ธฐ)

 select hiredate
           , to_char (hiredate, 'YYYY') as "๋ฌธ์ž ๋…„๋„ ํ‘œ๊ธฐ"
           , to_char (hiredate, 'YYYY/MM/DD')  as "๋ฌธ์ž ๋…„/์›”/์ผ ํ‘œ๊ธฐ"
           , to_char(hiredate, 'DAY') as "์š”์ผ"
from emp;

 

 

 

2.To_char๋ฅผ ํ†ตํ•œ ์…€ ์„œ์‹ ๋ณ€๊ฒฝ  

select sal
          , to_char(sal, '999,999')  as  "์‰ผํ‘œ์ฒ˜๋ฆฌ"
          , to_char(sal,'$999,999') as  "$๊ธฐํ˜ธ + ์‰ผํ‘œ์ฒ˜๋ฆฌ"
from emp ;

 

 

 

3. To_char & Extract์˜ ์ฐจ์ด์  

  • ๋‚ ์งœ ๋ฐ์ดํ„ฐ → ๋ฌธ์ž ๋ฐ์ดํ„ฐ (To_char)
  • ๋‚ ์งœ ๋ฐ์ดํ„ฐ → ๋‚ ์งœ ์›”๋งŒ ์ถ”์ถœ (Extract)

select  hiredate
          , to_char (hiredate, 'MM') as "(๋ฌธ์ž) ์›” ํ‘œ๊ธฐ" 
          , extract(month from hiredate) as "(์ˆซ์ž) ์›” ์ถ”์ถœ"
from emp ;

 

 

 

 

4. ๋ฌธ์ œ : "12์›”์— ์ž…์‚ฌํ•œ ์‚ฌ๋žŒ๋“ค"์˜ ์ด๋ฆ„๊ณผ, ์ž…์‚ฌ์ผ์„ ์กฐํšŒํ•˜๊ณ  ์‹ถ๋‹ค.

 

(4.1) Where ์ ˆ์— To_char ์‚ฌ์šฉ 

select ename, hiredate
from emp
where to_char(hiredate,'MM') = '12' ; โ—€ ๋ฌธ์ž ๋ฐ์ดํ„ฐ ์ด๊ธฐ์— '12'๋กœ ํ‘œ๊ธฐ

 

 

 

(4.2) Where ์ ˆ์— Extract ์‚ฌ์šฉ 

select ename, hiredate
from emp
where extract(month from hiredate) = 12 ; โ—€ ์ˆซ์ž ๋ฐ์ดํ„ฐ ์ด๊ธฐ์— 12๋กœ ํ‘œ๊ธฐ