본문 바로가기
데이터베이스/Mysql

[MySQL] SUBSTR(), DAYOFWEEK() 날짜 데이터에서 요일 출력하기

by 책 읽는 개발자_테드 2020. 3. 23.
반응형

SUBSTR

 

문자열을 추출하는 함수입니다.

 

다음과 같은 형태로 사용할 수 있습니다.

SUBSTRING(str,pos), SUBSTRING(str FROM pos), 

SUBSTRING(str,pos,len), SUBSTRING(str FROM pos FOR len)

 

len 매개변수는 pos 위치 부터 시작해서 str 문자열의 하위 문자열을 return 합니다!

 

예시

mysql> SELECT SUBSTRING('Quadratically',5); -> 'ratically'

mysql> SELECT SUBSTRING('foobarbar' FROM 4); -> 'barbar'

mysql> SELECT SUBSTRING('Quadratically',5,6); -> 'ratica'

mysql> SELECT SUBSTRING('Sakila', -3); -> 'ila' mysql> SELECT SUBSTRING('Sakila', -5, 3); -> 'aki'

mysql> SELECT SUBSTRING('Sakila' FROM -4 FOR 2); -> 'ki'

 

 

 

DAYOFWEEK

 

요일의 인덱스를 return하는 함수입니다(1 = 일요일, 2 = 월요일, 3 = 화요일 ..., 7 = 토요일).

 

예시

mysql> SELECT DAYOFWEEK('2007-02-03'); -> 7

 

 

날짜 데이터에서 요일 출력하기

 

위의 두 함수를 이용해서 날짜 데이터를 담고 있는 rsrv_day 필드의 값에 해당하는 요일을 출력한다면,  다음과 같이 표현할 수 있습니다.

 

SELECT rsrv_day, SUBSTR('일월화수목금토', DAYOFWEEK(RSRV_DAY), 1) AS day_of_the_week FROM 테이블 

 

 

출처

https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_substring

 

MySQL :: MySQL 8.0 Reference Manual :: 12.7 String Functions and Operators

12.7 String Functions and Operators Table 12.11 String Functions and Operators Name Description ASCII() Return numeric value of left-most character BIN() Return a string containing binary representation of a number BIT_LENGTH() Return length of argument in

dev.mysql.com

https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html

 

MySQL :: MySQL 8.0 Reference Manual :: 12.6 Date and Time Functions

12.6 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 in wh

dev.mysql.com

 

반응형

댓글