[데이터 베이스] 3. SELECT 단일행 함수

귤's avatar
Feb 25, 2025
[데이터 베이스] 3. SELECT 단일행 함수

1. 날짜/시간 기본

💡
now () = 함수
select now();
notion image
select ename, hiredate, now() from emp;
notion image
select '2025-02-25';
notion image
select date('2025-02-25 12:30:35'); select time('2025-02-25 12:30:35'); select year('2025-02-25 12:30:35'); select month('2025-02-25 12:30:35'); select day('2025-02-25 12:30:35'); select hour('2025-02-25 12:30:35'); select minute('2025-02-25 12:30:35'); select second('2025-02-25 12:30:35');
notion image
notion image
notion image
 
notion image
notion image
notion image
notion image
notion image

2. 날짜 포맷

날짜 포맷 키 값
select date_format(now(), '%Y/%m/%d %h:%i:%s');
notion image

3. 날짜 연산하기 (더하기, 빼기, 간격, 마지막 날짜)

1) 더하기

select date_add(now(), interval 4 YEAR); select date_add(now(), interval 4 MONTH); select date_add(now(), interval 4 WEEK); select date_add(now(), interval 4 day); select date_add(now(), interval 4 HOUR); select date_add(now(), interval 4 MINUTE); select date_add(now(), interval 4 SECOND);
notion image
notion image

2) 빼기

  • 날짜 빼기
select date_sub('2025-02-25', interval 4 day);
notion image

3) 간격 (차이)

  • 날짜 차이
select datediff('2025-02-25', '2025-03-01');
notion image
  • 시간 차이
select timediff(now(), '2020-02-25 12:50:00');
notion image

4) 마지막 날짜

select last_day(now());
notion image
notion image

4. 수학 함수

select floor(101.5);
notion image
select ceil(101.5);
notion image
select round(101.5);
notion image
select mod(101,5);
notion image

5. 문자열 함수

💡
substr(시작번지 1~, 개수)
select substr(hiredate,1,1) from emp;
notion image
 
select year(hiredate) from emp;
notion image
  • replace () : /를 -로 바꿈
select replace("010/2222/7777","/","-");
notion image
 
select instr("abcde", "c");
notion image
select rpad('ssarmango', 10, '*');
notion image
  • 문자열 길이 함수
    • LENGTH (str) → 바이트(Byte) 단위 길이 반환
    • 문자열의 바이트 길이를 반환한다.
    • 함수
      반환 값
      설명
      LENGTH(str)
      바이트 수
      UTF-8에서 한글 1글자 = 3바이트
      CHAR_LENGTH(str)
      문자 개수
      실제 글자 개수를 반환
select rpad(substr('ssarmango',1,4), LENGTH("ssarmango"), '*'); select lpad(substr('ssarmango',1,4), LENGTH("ssarmango"), '*');
notion image
notion image

6. 문제 풀기

💡
전화번호 가운데에 들어가 있는 숫자를 *로 바꿔보기
select * from student; select name, replace(tel, '381','***') from student;
정답
-- 6. 문제 풀기 select * from student; select name, replace(tel, '381','***') from student; select name, tel, substr(tel, instr(tel, ")")+1, instr(tel, "-")-instr(tel,")")-1) from student; select name, tel, LENGTH(substr(tel, instr(tel, ")")+1, instr(tel, "-")-instr(tel,")")-1)) from student; select name, tel, repeat('*',LENGTH(substr(tel, instr(tel, ")")+1, instr(tel, "-")-instr(tel,")")-1))) from student; select name, tel, replace(tel, substr(tel, instr(tel, ")")+1, instr(tel, "-")-instr(tel,")")-1), repeat('*',LENGTH(substr(tel, instr(tel, ")")+1, instr(tel, "-")-instr(tel,")")-1)))) from student; // -> 여기가 정답 select instr('031)2222-5555', ')'); select instr('031)2222-5555', '-'); select instr('031)2222-5555', ')'); -- 4 (4+1) select substr('031)2222-5555', '-'); -- 9 (9-4-1) select instr('02)333-5555', '-'); select substr('02)333-5555', 4,3); -- 3+1, 7-3-1 select name, tel, replace(tel, substr(tel, 5,3), "***") from student;
notion image

7. 조건문

-- 7. 조건문 (if - MySQL, case - when, 모든 DB) select if(10>5, "참", "거짓"); -- 2500 (고액연봉), (일반연봉) select ename, sal, case when sal>2500 then "고액연봉" when sal>2000 then "일반연봉" else "중간연봉" end "연봉그룹" from emp;
notion image

8. 정렬

1) 오름 차순

select * from emp where deptno = 20 order by sal asc;
notion image
💡
asc = 오름 차순

2) 내림 차순

select * from emp where deptno = 20 order by sal desc;
notion image

3) 이름이 오름차순 이면서, 가격은 내림차순으로, 둘이 동시에 진행할 때

select * from emp where deptno= 20 order by ename asc, sal desc;
notion image
Share article

gyul