-
SQL 명령어 정리SQL 명령어 정리 2019. 7. 25. 22:00반응형
ctrl + f 키를 눌러 검색
1. SQL 주석 처리하는 법
1.1 SQL 한줄 처리 : -- 사용
-- sql 주석 처리 select * from sample_table
1.2 SQL 여러줄 처리 : /* */ 사용
/* 여러줄 주석 처리 */ select * from sample_table
2. SQL 변환/바꾸기 : replace(변환하고 싶은 글자나 칼럼명, '해당 문자', '변경할 문자')
ex) sample_date 칼럼에서 " 제거
select replace(order_date, '"', '') from sample_table
3. SQL 날짜 관련 원하는 년, 월, 일, 시, 분, 초, 요일 뽑아내는 법
strftime 명령어 와 소대문자를 조심하며 순서대로 %Y, %m, %d, %H, %M, %S, %w(0:일요일,~,6: 토요일) 를 함께 입력
-- 년도 select strftime('%Y','now') from sample_table -- 월 select strftime('%m','now') from sample_table -- 일 select strftime('%d','now') from sample_table -- 시 select strftime('%H','now') from sample_table -- 분 select strftime('%M','now') from sample_table -- 초 select strftime('%S','now') from sample_table -- 요일 select strftime('%w','now') from sample_table
3.1 SQL 날짜,시간 차이를 초로 나타내는 법
select strftime('%s', '2019-07-07 01:01:01') - strftime('%s', '2019-07-07 01:01:00') from sample_table
4. SQL 자르기 함수 : substrt(칼럼, 시작점, 시작점으로 읽을 문자 개수)
-- 날짜에서 년도 뽑기 select substr('20170101',1,4) from sample_table
5. SQL 조건문 여러개 : case when end 절 사용 (마지막에 end 사용하는것을 명심)
-- 문제: column n1 이 20 이상 이면 a, 10 이상 20미만이면 b, 해당되지 않으면 c 를 주어라 select case when n1 >=20 then 'a' when n1 >=10 and n1 < 20 then 'b' else 'c' end from sample_table
6. SQL 오름 차순 / 내림차순 : ASC / DESC 사용
오름차순 : 점점 커지는것 ex) 1, 2, 3
내림차순 : 점점 작아지는 것 ex) 3, 2, 1
-- emp_no 칼럼 오름차순 select * from sample_table order by emp_no asc -- emp_no 칼럼 내림차순 select * from sample_table order by emp_no desc
* todo
가나다 순으로 정리
스트링 -> 인트 컨버트
스트링 합치기
올림 / 반올림 / 내림
합산 하는 법
앞쪽 null 개수에 따른 평균 계산법
반응형'SQL 명령어 정리' 카테고리의 다른 글
SQL 실행 순서 (0) 2019.07.27