Oracle入门必会(7)

121. 取时间点的日的写法:
      SELECT TO_CHAR(SYSDATE,'DD') FROM DUAL;
122. 取时间点的时的写法:
      SELECT TO_CHAR(SYSDATE,'HH24') FROM DUAL;
123. 取时间点的分的写法:
      SELECT TO_CHAR(SYSDATE,'MI') FROM DUAL;
124. 取时间点的秒的写法:
      SELECT TO_CHAR(SYSDATE,'SS') FROM DUAL;
125. 取时间点的日期的写法:
      SELECT TRUNC(SYSDATE) FROM DUAL;
126. 取时间点的时间的写法:
      SELECT TO_CHAR(SYSDATE,'HH24:MI:SS') FROM DUAL;
127. 日期,时间形态变为字符形态:
      SELECT TO_CHAR(SYSDATE) FROM DUAL;
128. 将字符串转换成日期或时间形态:
      SELECT TO_DATE('2003/08/01') FROM DUAL;
129. 返回参数的星期几的写法:
      SELECT TO_CHAR(SYSDATE,'D') FROM DUAL;
130. 返回参数一年中的第几天的写法:
      SELECT TO_CHAR(SYSDATE,'DDD') FROM DUAL;
131. 返回午夜和参数中指定的时间值之间的秒数的写法:
      SELECT TO_CHAR(SYSDATE,'SSSSS') FROM DUAL;
132. 返回参数中一年的第几周的写法:
      SELECT TO_CHAR(SYSDATE,'WW') FROM DUAL;
虚拟字段
133. CURRVAL 和 nextval
     为表创建序列
     CREATE SEQUENCE EMPSEQ ... ;
     SELECT empseq.currval FROM DUAL ;
     自动插入序列的数值
     INSERT INTO emp
         VALUES (empseq.nextval, 'LEWIS', 'CLERK',
                 7902, SYSDATE, 1200, NULL, 20) ;
134. ROWNUM
     按设定排序的行的序号
     SELECT * FROM emp WHERE ROWNUM < 10 ;
135. ROWID
     返回行的物理地址
     SELECT ROWID, ename FROM emp  WHERE deptno = 20 ;
136. 将N秒转换为时分秒格式?
     set serverout on
     declare
       N number := 1000000;
       ret varchar2(100);
     begin
       ret := trunc(n/3600) || '小时' || to_char(to_date(mod(n,3600),'sssss'),'fmmi"分   "ss"秒"') ;
       dbms_output.put_line(ret);
     end;
137. 如何查询做比较大的排序的进程?
     SELECT b.tablespace, b.segfile#, b.segblk#, b.blocks, a.sid, a.serial#, a.username, a.osuser, a.status
     FROM v$session a,v$sort_usage b
     WHERE a.saddr = b.session_addr
     ORDER BY b.tablespace, b.segfile#, b.segblk#, b.blocks ;
138. 如何查询做比较大的排序的进程的SQL语句?
     select /*+ ORDERED */ sql_text from v$sqltext a
     where a.hash_value = (
       select sql_hash_value from v$session b
       where b.sid = &sid and b.serial# = &serial)
     order by piece asc ;
139. 如何查找重复记录?
     SELECT * FROM TABLE_NAME
     WHERE ROWID!=(SELECT MAX(ROWID) FROM TABLE_NAME D
       WHERE TABLE_NAME.COL1=D.COL1 AND TABLE_NAME.COL2=D.COL2);
140. 如何删除重复记录?
     DELETE FROM TABLE_NAME
     WHERE ROWID!=(SELECT MAX(ROWID) FROM TABLE_NAME D
     WHERE TABLE_NAME.COL1=D.COL1 AND TABLE_NAME.COL2=D.COL2);

你可能感兴趣的:(Oracle入门必会(7))