본문으로 바로가기
728x90

오늘 특정 테이블에 VARCHAR 타입인 컬럼을 DATETIME 포맷으로 변경해 넣어야 하는 작업이 발생했다.



- a_list 테이블의 정보를 b_info 테이블로 옮겨야 했다.


"Sun, 16 Nov 2014 15:00:00 GMT"

"Fri, 09 Nov 2018 15:00:00 GMT"


이런 형식의 데이터를 STR_TO_DATE() 함수를 사용해 DATETIME 형태로 변환하여 넣을수 있었다.

INSERT INTO a_info (published_date)
SELECT STR_TO_DATE(b_list.publish_date, '%a, %d %b %Y') FROM b_list;


"Sun, 16 Nov 2014 15:00:00 GMT"    >>> "2014-11-16"
"Fri, 09 Nov 2018 15:00:00 GMT"     >>> "2018-11-09"


아래는 mysql 공식 홈페이지에서 발췌해온 표입니다. (대소문자를 구별합니다.)


SpecifierDescription
%aAbbreviated weekday name (Sun..Sat)
%bAbbreviated month name (Jan..Dec)
%cMonth, numeric (0..12)
%DDay of the month with English suffix (0th1st2nd3rd, …)
%dDay of the month, numeric (00..31)
%eDay of the month, numeric (0..31)
%fMicroseconds (000000..999999)
%HHour (00..23)
%hHour (01..12)
%IHour (01..12)
%iMinutes, numeric (00..59)
%jDay of year (001..366)
%kHour (0..23)
%lHour (1..12)
%MMonth name (January..December)
%mMonth, numeric (00..12)
%pAM or PM
%rTime, 12-hour (hh:mm:ss followed by AM or PM)
%SSeconds (00..59)
%sSeconds (00..59)
%TTime, 24-hour (hh:mm:ss)
%UWeek (00..53), where Sunday is the first day of the week; WEEK()mode 0
%uWeek (00..53), where Monday is the first day of the week; WEEK()mode 1
%VWeek (01..53), where Sunday is the first day of the week; WEEK()mode 2; used with %X
%vWeek (01..53), where Monday is the first day of the week; WEEK()mode 3; used with %x
%WWeekday name (Sunday..Saturday)
%wDay of the week (0=Sunday..6=Saturday)
%XYear for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%xYear for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%YYear, numeric, four digits
%yYear, numeric (two digits)
%%A literal % character
%xx, for any x not listed above


728x90