MySQL
MySQL - 날짜와 시간 처리 datetime테이블 설정 및 원하는 연,월,일,요일 출력
왕현성
2022. 12. 7. 16:49
728x90
테이블을 만들때 Datatype을 설정해줍니다.
DATE는 날짜만 출력하고 TIME은 시간만 출력, DATETIME은 두개 전부 출력합니다.
use YHDB;
insert into people2 (name,birthdate,birthtime,birthdt)
values ('Mike','1990-11-11','10:07:35','1990-11-11 10:07:35'),
('Larry','1972-12-25','04:10:42','1972-12-25 04:10:42');
select *
from people2;
-- 날짜 정보만 가져오기
select name,day(birthdate)
from people2;
select name,dayname(birthdate) -- 해당 날짜의 요일 출력
from people2;
select name,dayofweek(birthdate) -- 해당 날짜의 요일을 숫자로 출력
from people2;
select name,dayofyear(birthdate) -- 해당 날짜가 1년중 며칠이 지났는지 확인
from people2;
select name, month(birthdate) -- 해당 월
from people2;
select name, day(birthdate) -- 해당 일
from people2;
select name, year(birthdate) -- 해당 년
from people2;
select name, hour(birthtime) -- 해당 시간
from people2;
select name, minute(birthtime) -- 해당 분
from people2;
select name, second(birthtime) -- 해당 초
from people2;