7. SQL 함수 (날짜&수학&문자열)

박은서's avatar
Apr 24, 2026
7. SQL 함수 (날짜&수학&문자열)

1. SQL 함수 (날짜&수학&문자열)

1️⃣ 날짜

1) now()

현재 시간 출력
select now();
notion image

2) year(날짜)

select year('2025-12-23');
notion image

3) month(날짜)

select month('2025-12-23');
notion image

4) day(날짜)

select day('2025-12-23');
notion image

5) hour(날짜 시간)

select hour('2025-12-23 12:36:25');
notion image

6) second(날짜 시간)

select second('2025-12-23 12:36:25');
notion image

2️⃣ 날짜 포맷

1) 포맷 기호

기호
의미
예시
%Y
연도(4자리)
2025
%y
연도(2자리)
25
%m
월(2자리)
01 ~ 12
%c
월(숫자)
1 ~ 12
%d
일(2자리)
01 ~ 31
%e
일(숫자)
1 ~ 31
%H
시(24시간)
00 ~ 23
%h
시(12시간)
01 ~ 12
%i
00 ~ 59
%s
00 ~ 59

2) 실습

select date_format(날짜, 포맷방법);
select date_format(now(),'%Y');
notion image
select date_format(now(),'%y');
notion image
select date_format(now(),'%Y/%m');
notion image
select date_format(now(),'%Y/%m/%d-%h:%i:%s');
notion image

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

1) 더하기

select date_add(now(), interval 4 year);
notion image
select date_add(now(), interval 4 month);
notion image
select date_add(now(), interval 4 week);
notion image
select date_add(now(), interval 100 day);
notion image
select date_add(now(), interval 500 hour);
notion image
select date_add(now(), interval 5200 minute);
notion image
select date_add(now(), interval 4 second);
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

4) 시간 차이

select timediff(now(), '2025-12-23 12:40:00');
notion image

5) 현재 달의 마지막 날짜

select last_day(now());
notion image

4️⃣ 수학 함수

1) 내림

select floor(101.5);
notion image

2) 올림

select ceil(101.5);
notion image

3) 반올림

select round(101.5);
notion image

4) 나머지 구하기

select mod(101, 10);
notion image

5️⃣ 문자열 함수

1) substr(문자열, 시작번지(1~), 개수)

select substr(hiredate,1,4) from emp;
notion image
select substr(hiredate,6,2) from emp;
notion image

2) replace(문자열, 바꿀 문자, 치환할 문자)

select replace('010/2222/7777', '/', '-');
notion image

3) instr(문자열, 위치 확인할 문자)

select instr('abcde', 'c');
notion image

4) rpad(문자열, 표현할 문자 길이, 빈칸 채울 문자)

select rpad('ssarmango', 10, '*');
notion image
select rpad(substr('ssarmango',1,4), LENGTH("ssarmango"), '*');
notion image
select lpad(substr('ssarmango',1,4), LENGTH("ssarmango"), '*');
notion image
 
Share article