[DB] 3. SELECT 단일 행 함수

최재원's avatar
Feb 26, 2025
[DB] 3. SELECT 단일 행 함수

1. 날짜/시간

1. 현재 시간

select now();
notion image
select ename, hiredate, now() from emp;
notion image

2. 날짜 포멧

1. 년-월-일

select date('2025-02-25 12:30:35'); -- 날짜
notion image

2. 시:분:초

select time('2025-02-25 12:30:35'); -- 시간
notion image

3. 년도

select year('2025-02-25 12:30:35'); -- 년도
notion image

4. 월

select month('2025-02-25 12:30:35'); -- 월
notion image

5. 일

select day('2025-02-25 12:30:35'); -- 일
notion image

6. 시간

select hour('2025-02-25 12:30:35'); -- 시간
notion image

7. 분

select minute('2025-02-25 12:30:35'); -- 분
notion image

8. 초

select second('2025-02-25 12:30:35'); -- 초
notion image
날짜 포멧
형식 지정자
의미
예시
%Y
4자리 연도
2024
%y
2자리 연도
24
%m
2자리 월
02 (2월)
%c
1~2자리 월
2 (2월)
%d
2자리 일
07
%e
1~2자리 일
7
%H
24시간제 시간
15
%h / %I
12시간제 시간
03
%i
30
%s
45
%p
AM/PM
AM 또는 PM
%W
요일(영어)
Monday
%a
요일(짧게)
Mon
%w
요일(숫자, 0=일)
1 (월요일)
%M
월(영어)
February
%b
월(짧게)
Feb
%j
연중 몇 번째 날
038
%U
연중 몇 번째 주 (일요일 시작)
06
%V
연중 몇 번째 주 (월요일 시작)
06

 
select date_format(now(),'%y/%m/%d-%H:%i:%s');
notion image

3. 날짜 연산하기

1. 더하기 date_add()

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);

2. 빼기 date_sub()

select date_sub('2025-02-25', interval 4 day); -- 문자열도 지원함

3. 간격 diff()

select datediff('2025-02-25', '2025-03-01'); select timediff(now(), '2025-02-25 12:50:00');

4. 마지막 날짜 last_day()

select last_day(now());

2. 수학 함수

1. 반올림 및 절삭 함수

select floor(101.5); -- 내림 select ceil(101.5); -- 올림 select round(101.5); -- 반올림 select truncate(10.231231, 3); -- 소숫점 버림

2. 나머지 및 나눗셈 함수

select mod(101,10); -- 나머지 select 101 % 10; -- 나머지 select 101 div 10; -- 나눈 몫

3. 절대값

select abs(-10);

3. 문자열 함수

1. 문자열 추출 및 잘라내기 함수

select substr('Hello', 2, 3); -- 잘라내기
  • 시작 번지
  • 잘라낼 개수

2. 문자열 조작 함수

select replace('010/2222/7777', '/', '-'); -- 문자열 대체 select repeat('*', 5); -- 문자열 반복 select reverse('abc'); -- 문자열 뒤집기

3. 문자열 검색 함수

select instr('abcde','c'); -- 특정 문자열 인덱스 반환

4. 문자열 길이 함수

select length('abc'); -- 문자열 길이

5. 공백 제거 및 패딩 함수

select rpad('abc', 6, 0); -- 오른쪽 공백 채우기 select lpad('abc', 6, 0); -- 왼쪽 공백 채우기 select trim(' abc '); -- 양쪽 공백 제거

4. 조건문

1. if

select if(10 > 5, '참', '거짓');
notion image

2. case when

select ename, sal, case when sal > 2500 then "고액연봉" when sal < 2000 then "일반연봉" else "중간연봉" end "연봉그룹" from emp;
notion image

5. 정렬

select * from emp where deptno = 20 order by sal asc;
notion image
select * from emp where deptno = 20 order by sal desc;
notion image
select * from emp where deptno = 20 order by hiredate, sal desc; -- 복수 가능

6. 문제 풀기

notion image
  • tel 의 가운데 번호를 *로 대체하라.

1. 가운데 번호를 찾을 인덱스 찾기

select instr('02)318-5440', ')'); -- 시작 인덱스 3 select instr('02)318-5440', '-'); -- 끝 인덱스 7 select instr('02)318-5440', ')') - instr('02)318-5440', '-'); -- 간격 -4 select abs(instr('02)318-5440', ')') - instr('02)318-5440', '-')); -- 간격 절대값 4

2. 가운데 번호 찾기

select substr('02)318-5440', 4, 3); -- 시작번호 다음 숫자부터 끝번호 전까지 - 가운데 번호 select instr('02)318-5440', ')') + 1; -- 시작 인덱스 + 1 select abs(instr('02)318-5440', ')') - instr('02)318-5440', '-')) - 1; -- 간격 절대값 - 1 = 개수 select substr( '02)318-5440', instr('02)318-5440', ')') + 1, abs(instr('02)318-5440', ')') - instr('02)318-5440', '-')) - 1 ); -- 중간 번호 선택
notion image

3. 별 반복 하기

select repeat('*', 3); -- 별 반복 select repeat( '*', abs(instr('02)318-5440', ')') - instr('02)318-5440', '-')) - 1 ); -- 개수 만큼 별 반복
notion image

4. 마지막 확인

select replace( '02)318-5440', substr('02)318-5440', instr('02)318-5440', ')') + 1, abs(instr('02)318-5440', ')') - instr('02)318-5440', '-')) - 1), repeat('*', abs(instr('02)318-5440', ')') - instr('02)318-5440', '-')) - 1) );
notion image

5. 최종

select name, replace( tel, substr(tel, instr(tel, ')') + 1, abs(instr(tel, ')') - instr(tel, '-')) - 1), repeat('*', abs(instr(tel, ')') - instr(tel, '-')) - 1) ) '가린 번호' from student;
notion image
 
Share article

jjack1