본문 바로가기
STUDY_NOTE/sql

[Mysql] 날짜, 시간 표기 방식 정리

by kh201 2021. 3. 25.

다음과 같이 id 와 datetime 2열로 이루어진 간단한 테이블을 생성하고

 

 

임의로 날짜와 시간을 만들어 넣자.

 

insert into sandbox2 (datetime) VALUES

('2017-08-28 17:22:21'),

('2017-02-15 10:22:24'),

('2017-12-09 22:13:24'),

('2017-07-06 20:15:18'),

('2017-11-19 23:13:19'),

('2017-03-06 19:19:11'),

('2017-03-07 19:18:13'),

('2017-10-01 19:18:15'),

('2017-07-02 20:21:21'),

('2017-11-20 23:16:23'),

('2017-12-11 14:24:20'),

('2017-12-21 12:16:14');

 

이제 다음과 같이 질의하면

 

SELECT datetime FROM `sandbox2` WHERE id=1;

 

다음과 같이 나올 텐데

 

 

이 결과에서 시간 없이 날짜만 표기할 수 있을까?

 

date_format() 을 쓰면 가능하다.

 

예를 들어 다음과 같이 입력하면

 

SELECT date_format(datetime, '%Y-%m-%d') FROM `sandbox2` WHERE id=1;

 

다음과 같이 날짜만 나온다.

 

 

date_format의 두 번째 항목인 '%Y-%m-%d' 가 형식을 지정하는 부분임을 짐작할 수 있다.

 

여기에 쓰일 수 있는 목록은 다음과 같으며,

 

출처인 이곳에서 더 자세한 내용을 확인해 볼 수 있다.

(https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format)

 

 

Specifier

 Description

%a

 Abbreviated weekday name (Sun..Sat)

%b

 Abbreviated month name (Jan..Dec)

%c

 Month, numeric (0..12)

%D

 Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)

%d

 Day of the month, numeric (00..31)

%e

 Day of the month, numeric (0..31)

%f

 Microseconds (000000..999999)

%H

 Hour (00..23)

%h

 Hour (01..12)

%I

 Hour (01..12)

%i

 Minutes, numeric (00..59)

%j

 Day of year (001..366)

%k

 Hour (0..23)

%l

 Hour (1..12)

%M

 Month name (January..December)

%m

 Month, numeric (00..12)

%p

 AM or PM

%r

 Time, 12-hour (hh:mm:ss followed by AM or PM)

%S

 Seconds (00..59)

%s

 Seconds (00..59)

%T

 Time, 24-hour (hh:mm:ss)

%U

 Week (00..53), where Sunday is the first day of the week; WEEK() mode 0

%u

 Week (00..53), where Monday is the first day of the week; WEEK() mode 1

%V

 Week (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X

%v

 Week (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x

%W

 Weekday name (Sunday..Saturday)

%w

 Day of the week (0=Sunday..6=Saturday)

%X

 Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V

%x

 Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v

%Y

 Year, numeric, four digits

%y

 Year, numeric (two digits)

%%

 A literal % character

%x

 x, for any “x” not listed above

 

그러므로 만일 시간만, 12시간제로 뽑고 싶다면 다음과 같이 적으면 되겠다. (대소문자 구별에 주의한다)

 

SELECT date_format(datetime, '%h:%i:%s') FROM `sandbox2` WHERE id=1

lightblog.tistory.com/155

 

[MySQL] 날짜, 시간 표기 방식 지정하기 DATE_FORMAT()

다음과 같이 id 와 datetime 2열로 이루어진 간단한 테이블을 생성하고 임의로 날짜와 시간을 만들어 넣자. insert into sandbox2 (datetime) VALUES ('2017-08-28 17:22:21'), ('2017-02-15 10:22:24'), ('2017-..

lightblog.tistory.com

https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format

 

MySQL :: MySQL 5.7 Reference Manual :: 12.7 Date and Time Functions

12.7 Date and Time Functions This section describes the functions that can be used to manipulate temporal values. See Section 11.2, “Date and Time Data Types”, for a description of the range of values each date and time type has and the valid formats

dev.mysql.com

 

'STUDY_NOTE > sql' 카테고리의 다른 글

사용자정의함수  (0) 2021.04.14
mysql 날짜 1일 더하기 빼기  (0) 2021.04.13
시간 계산 함수  (0) 2021.03.23
시간 계산 함수  (0) 2021.03.23
mysql varchar를 time 으로 형변환  (0) 2021.03.23