티스토리 뷰

728x90

날짜함수 

date_format()에서 대소문자에 따라 표기되는 결과가 달라질 수 있다.

select now();
select curdate();
select curtime();
2021-11-08 05:19:46
2021-11-08
05:20:14
select date_format(now() , '%Y-%m-%d %H:%i:%s');
select date_format(now(), '%Y-%m-%d');
select date_format(now(), '%Y.%m.%d %H:%i:%s')
2021-11-08 03:36:37 // YYYY-MM-DD HH:MM:SS 
2021-11-08 // YYYY-MM-DD
2021.11.08 03:40:38 // YYYY.MM.DD HH:MM:SS
select date_add(now(), interval 1 hour);
select date_add(now(), interval 1 day);
select date_add(now(), interval 1 month);
select date_add(now(), interval 1 year);
select date_add(now(), interval -1 year);
select date_sub(now(), interval 1 year);
2021-11-08T04:52:09Z // 현재시간 +1시간
2021-11-09T03:54:20Z // 현재시간 +1일
2021-12-08T03:55:25Z // 현재시간 +1개월
2022-11-08T03:56:38Z // 현재시간 +1년
2020-11-08T03:57:19Z // 현재시간 -1년
2020-11-08T03:57:19Z // 현재시간 -1년
select datediff('2021-11-08', '2021-12-08') 30 // datediff(a,b) - b-a의 값을 가져옴 (mssql과 반대)
select weekday(str_to_date('20211108','%Y%m%d'))
select dayname(now())
0 // 요일구하기- 0 (월0/화1/수2/목3/금4/토5/일6) 
월요일 // dayname은 월~일 요일을 가져옴
   
   

%Y - 연도 YYYY (4자리 표기)

%y - 연도 yy (2자리 표기)

%M - 월 이름 (January ~ December)

%m - 월 숫자 (00 ~ 12)

%d - 일 (00 ~31)

%H - 시간 24시간 (00~23)

%h - 시간 12시간 (00~12)

%i - 분 (00~59)

%s - 초 (00~59)

 

타입변환

select cast(10.1234 as decimal(18,2)) 10.12 //소수점 2자리 변환

 

문자열

select char_length('안녕')
select length('안녕');
2
6
select instr("1234567890", "1");
select instr("1234567890", "90");
1
9
select ifnull(null,'hi') hi
select concat('hello', ' ', 'wor', 'ld', '!')
select concat_ws('//','hello', ' ', 'wor', 'ld', '!')
hello world!
hello// //wor//ld//! -->첫번째 인자는 구분자
select substring('hello', 1, 2)
select left('hello', 3)
select right('hello', 3)
he //1번째 자리부터 2개 자름
hel
llo

//www
select substring_index('www.helloworld.com', '.', 1);
//www.helloworld 
select substring_index('www.helloworld.com', '.', 2);
//com
select substring_index('www.helloworld.com', '.', -1);  
//helloworld.com
select substring_index('www.helloworld.com', '.', -2);  
[substring_index(str, delim, count)]

www // str의 왼쪽부터 delim이 1번째 나오는 곳까지의 문자열을 리턴한다.
www.helloworld // str의 왼쪽부터 delim이 2번째 나오는 곳까지의 문자열을 리턴한다.
com // str의 오른쪽부터 delim이 1번째 나오는 곳까지의 문자열을 리턴한다.
helloworld.com // str의 오른쪽부터 delim이 2번째 나오는 곳까지의 문자열을 리턴한다.

select upper('hello');
select lower('HELLO');
HELLO
hello
select replace('www.helloworld.com', 'hello', 'hi'); www.hiworld.com
select repeat('a',10) aaaaaaaaaa
select rpad('hello', 10, '123');
select lpad('hello', 10, '123');
rpad (원본 문자열, 최종 문자열 길이, 추가할 문자열)
hello12312 //원본 문자열 오른쪽으로 추가할 문자열을 최종문자열 길이만큼 반복
12312hello // 원본 문자열 왼쪽으로 추가할 문자열을 최종문자열 길이만큼 반복
select rtrim('   abc   ')
select ltrim('   abc   ')
select trim('   abc   ')
왼쪽/오른쪽 공백 자름
select reverse('abc'); cba

 

수치연산

select abs(-10); 10 //절대값
select ceiling(10.234); 11 //올림
select floor(10.543); 10 //내림
select round(10.547,1);
select round(10.547,2);
10.5 //소수점 1자리까지 반올림
10.55 //소수점 2자리까지 반올림
select format(274000,0);
select format(274000,1);
select format(274000,2);
274,000 //1000단위 콤마
274,000.0 //1000단위 콤마, 소수점1자리 0
274,000.00 //1000단위 콤마, 소수점2자리 0
select mod(5,3); 2 // 나머지 연산 (mssql %와 동일)
select pow(2,3); 8 // 2^3 = 2*2*2 
select sqrt(4);
select sqrt(10);
2 // 4의 제곱근 반환
3.1622776601683795
select rand(); 0.19115230738292663 //랜덤값

 

 

https://www.w3schools.com/sql/sql_ref_mysql.asp

 

MySQL Functions

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

728x90
LIST

'Programming > MySQL' 카테고리의 다른 글

[programmers] MySQL 코딩연습  (0) 2021.11.14
[LeetCode] MySQL 코딩연습  (0) 2021.11.13
[MySQL] MySQL vs MSSQL 함수 비교  (0) 2021.11.08
[MSSQL/Hackerrank] 15 Days of Learning SQL  (0) 2021.11.07
[MySQL/Hackerrank] Print Prime Numbers  (0) 2021.11.07
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함