본문 바로가기

I T./Oracle

오라클 SQL

- 컬럼의 결합
SQL> select first_name || last_name employee from emp;

 


- 중복데이터 찾기
SQL> select * from dept a where 1<(select count(deptno) from dept b where a.deptno=b.deptno);

 


- 중복된 row를 빼고 출력
SQL> select distinct name from dept;

 


- 데이터정렬
SQL> select last_name,dept_id,start_date from s_emp order by last_name;
SQL> select last_name,dept_id,start_date from s_emp order by 1;
(인덱스값이1인 컬럼=last_name기준으로 정렬되며 조건생략시 asc(오름차순))

 


- 정렬에서 null값 처리
  order by b.part_kind asc nulls last
  order by b.part_kind asc nulls first

 


- 범위지정 between...and
SQL> select first_name,last_name,start_date from s_emp
where start_date between '20070131' and '20070228';

 


- ㄱㄴㄷ으로 정렬하기
SQL> select count(*),substr(rowtohex(substr(name,1,1)),1,3) from userinformation group by substr(rawtohex(substr(name,1,1)),1,3);
(한글은 자음하나가 16진수 3자리로 구성이 되므로 헥사값으로 변환한 후에 group by 사용)

 


- space있는 값을 null로 비교
rtrim(a.ymd_myun) is null

 


- null값 처리(null값을 다른 값으로 대체)
SQL> select last_name,salary,*nvl(commission_pct,0)/100 commission from s_emp;

 


- 요일구하기
SQL> select to_char(to_date('20070505','yyyymmdd'),'d') from dual;
(해당요일이 숫자로 출력(0은 일요일)-0,1,2,3,4,5,6,7)
SQL> select to_char(to_date('20070505','yyyymmdd'),'day') from dual;
(해당요일이 한글로 출력-일요일,월요일,화요일,수요일,목요일,금요일,토요일)

 


- 선택한 row만 보여주기
SQL> select * from emp where rownum<=100;
- %와 _가 포함된 레코드 검색하기
  · _가 포함된 레코드 검색
SQL> select * from row_num where b like '%#_%' escape '#';
  · %가 포함된 레코드 검색
SQL> select * from row_num where b like '%|%%' escape '|';
  · %와 _가 포함된 레코드 검색
SQL> select * from row_num where b like '%|_%' escape '|' and b like '%#%%' escape '#';

 


- 3자리마다 쉼표 찍기
SQL> select trim(to_char(sal,'999'999'999'999')) from money;
(0으로 숫자를 바꿔주면 앞에 없는 초과한 값은 0으로 표시된다)


'I T. > Oracle' 카테고리의 다른 글

Oracle Backup  (0) 2009.12.10
Oracle 권한  (0) 2009.12.10
오라클 정리 From 송~  (0) 2009.12.10
ORACLE 9i ARCHITECTURE 아키텍쳐  (0) 2009.12.10
Oracle 함수  (0) 2009.12.09
Oracle SQL Select 명령어  (0) 2009.12.09