공부하면서 추가되는 MYSQL 함수

minseok·2023년 2월 21일
0

모든 내용은 MySql 8.0 문서를 기준으로 합니다.

포스팅 형식은 1개의 주제마다
참조되는 MySql 문서링크 + 학습과 업무에서 경험한 함수, 기능을 정리하면서 이어갑니다.











🐇 Date And Time

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





🐰 year(date)

연도 반환


Returns the year for date, in the range 1000 to 9999, or 0 for the “zero” date. Returns NULL if date is NULL.

1000 ~ 9999까지 반환되며 "0"은 0, NULL은 NULL을 반환합니다.

mysql> SELECT YEAR('1987-01-01');
-> 1987



🐰 date_format(date, format)

지정된 형식의 날짜 반환


Formats the date value according to the format string. If either argument is NULL, the function returns NULL.
The specifiers shown in the following table may be used in the format string. The % character is required before format specifier characters. The specifiers apply to other functions as well: STR_TO_DATE(), TIME_FORMAT(), UNIX_TIMESTAMP().

format인자에 따라 날짜 값의 형식을 지정합니다.
두개의 인자중에서 1개라도 NULL이라면 NULL을 반환합니다.

mysql> SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y');
-> 'Sunday October 2009'
mysql> SELECT DATE_FORMAT('2007-10-04 22:23:00', '%H:%i:%s');
-> '22:23:00'
mysql> SELECT DATE_FORMAT('1900-10-04 22:23:00',
-> '%D %y %a %d %m %b %j');

-> '4th 00 Thu 04 10 Oct 277'
mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00',
-> '%H %k %I %r %T %S %w');

-> '22 22 10 10:23:00 PM 22:23:00 00 6'
mysql> SELECT DATE_FORMAT('1999-01-01', '%X %V');
-> '1998 52'
mysql> SELECT DATE_FORMAT('2006-06-00', '%d');
-> '00'



🐰 datediff(expr1, expr2)

두개의 날짜 빼기


returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.

"expr1 - expr2"의 일수 형식의 연산 값을 반환합니다.
expr1, expr2 1개라도 NULL인 경우 NULL을 반환합니다.
비슷한 함수로 TIMEDIFF()가 있습니다.

mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');
-> 1
mysql> SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31');
-> -31


🐇 Mathematical Functions

https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html




🐰 round(X, D)

반올림


Rounds the argument X to D decimal places. The rounding algorithm depends on the data type of X. D defaults to 0 if not specified. D can be negative to cause D digits left of the decimal point of the value X to become zero. The maximum absolute value for D is 30; any digits in excess of 30 (or -30) are truncated. If X or D is NULL, the function returns NULL.

인수 X를 D 소수 자릿수로 반올림합니다.
D는 기본적으로 0의 값이며 반환 값은 첫 번째 인수와 동일한 유형을 가집니다.
또한 D의 범위는 -30 ~ 30, X혹은 D가 NULL인경우 NULL을 반환합니다.



반환 유형의 상세한 규칙

mysql> SELECT ROUND(-1.23);
-> -1
mysql> SELECT ROUND(-1.58);
-> -2
mysql> SELECT ROUND(1.58);
-> 2
mysql> SELECT ROUND(1.298, 1);
-> 1.3
mysql> SELECT ROUND(1.298, 0);
-> 1
mysql> SELECT ROUND(23.298, -1);
-> 20
mysql> SELECT ROUND(.12345678901234567890123456789012345, 35);
-> 0.123456789012345678901234567890



🐇 String Functions And Operators

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




🐰 left(str, len)

왼쪽부터 자르기

Returns the leftmost len characters from the string str, or NULL if any argument is NULL.

str을 왼쪽부터 len 값 만큼 얻습니다.
어떠한 인자들 NULL이면 NULL 반환

mysql> SELECT LEFT('foobarbar', 5);
-> 'fooba'

profile
즐겁게 개발하기

0개의 댓글