How old are you?

I just come back from holiday, I am quite busy at the moment. Here is a tiny function to get the age trunc((to_char(sysdate,’YYYYMMDD’)-to_char(birthdate,’YYYYMMDD’))/10000) it is much safer than add_months, because add_months do some conversion at the end of the month, and I would never accept to wait until Feb 29th, 2008 (28-2-1990 + 18*12 months) […]

unexpected results !

It makes you cry! It makes you claim you have found a bug! but it is working as specified! 1) subquery refers to a column of the main query select * from emp where ename in (select ename from dept where deptno=10); the query does not complain that column does not exist in dept. It […]

order by to_number ?

to_number is often subject to generate ORA-01722: invalid number. When I deal with integers, I prefer lpad. ex: sort pseudo-numeric select * from t order by lpad(col,20); 1 2 10 It performs well, and do not generate ora-01722. ora-01722 does not mean that I have bad data. select name,value from v$parameter where name like ‘%pool_size’ […]

shell + sqlplus

How to send commands to sqlplus ? Use stdin $ ( echo prompt $(hostname) $(date); echo desc emp ) | sqlplus -s scott/tiger dbsrv85a Mon Jun 6 17:01:46 CEST 2005 Name Null? Typ —- —– — EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) MGR NUMBER(4) HIREDATE DATE SAL NUMBER(7,2) COMM NUMBER(7,2) DEPTNO NUMBER(2) How […]

sys_connect_by_path

sys_connect_by_path is the only function provided by Oracle to get the the hierarchy path in one field, and it is only concatenating. I just found out a way of doing a sum of the path : let’s imagine I want the sum of the salary of all my hierarchy. select ename, sys_connect_by_path(ename,’/’) hierarchy, length(replace(sys_connect_by_path(lpad(‘ ‘,sal/10),’/’),’/’))*10 […]

Hierarchical queries

The typical hierarchical query is you want to select your boss, and the boss of your boss, etc. It could look like select prior ename ename, ename mgr from emp connect by prior mgr=empno start with ename=’SCOTT’; SCOTT SCOTT JONES JONES KING I start with Scott and the hierarchy is built. I can use the […]

Oracle analytics in basic sql queries

When I first saw analytics appearing in Oracle last century, I did not realised they were going to change my way of writting basic SQL queries. Some (Variance, deviance) are truely mathematical and still reserved for statistical analysis. Here I will try to describe ROW_NUMBER : Back in Oracle 7, I remember to have written […]