Tuesday, September 6, 2011

Comparing dates alphabetically

select 'april before january?' truth
from dual where
TO_CHAR(TO_DATE('10-04-2010','dd-mm-yyyy'), 'dd-mon-yyyy')
<
TO_CHAR(TO_DATE('10-01-1999', 'dd-mm-yyyy'), 'dd-mon-yyyy')

Tuesday, February 23, 2010

Rank on a date

SELECT trunc(car_modification_date,'MM'),
       car_no,
       rank() over (partition by car_no,trunc(car_modification_date,'MM') ORDER BY car_modification_date desc) rank     
  FROM histo_carte hc
 WHERE car_no = 1280
--
is not the same as
--
SELECT trunc(car_modification_date,'MM'),
       car_no,
       rank() over (partition by car_no,trunc(car_modification_date,'MM') ORDER BY  trunc(car_modification_date,'MM') desc) rank      
  FROM histo_carte hc
 WHERE car_no = 1280
--
RANK = 1 as the date is always the same...so the rank remains also the same...

Round a date to...

--
SELECT TRUNC(SYSDATE + 136, 'MM'), SYSDATE 
FROM dual
--

This rounds the date on the month, YYYY will round it on the year level...

Wednesday, December 9, 2009

Test positive to reject negative ?

I have an actual serious question for all you readers out there.

The question is to test for positive or test negative.
Example :

select 1 from some_table where some_flag = 0
/
select 1 from some_table where some_flag <> 1

Which is 'the best' and the most correct solution.
Bash away :)

Cheers.

Monday, November 9, 2009

DEFAULT NULL NOT NULL

--

ALTER TABLE my_table MODIFY my_column DEFAULT NULL NOT NULL
--

Friday, October 9, 2009

Round a negative to Zero

SELECT GREATEST(SOME_NEGATIVE_VALUE, 0) round_to_zero_val
FROM dual

Thursday, October 8, 2009

killer INSERT ALL WHEN

INSERT ALL
WHEN ( (select 1 from dual where 1=2) = 1) THEN
INTO zz_temp (a)
VALUES (1)
ELSE
INTO zz_temp (a)
VALUES (2)
SELECT 1 from dual;
/ -- Result ZZ_TEMP : 2

INSERT ALL
WHEN ( (select 1 from dual where 1=1) = 1) THEN
INTO zz_temp (a)
VALUES (1)
ELSE
INTO zz_temp (a)
VALUES (2)
SELECT 1 from dual;
/
-- Result ZZ_TEMP : 1