I change OCI8 version for PHP and since this query dosn\'t work :
SELECT \'M\'||to_char(to_date(OD.DATE2,\'DD/MM/YYYY\'),\'MM\') PERIODE, count(*) DATA, OD.D
Assuming that OD.DATE2
is of DATE datatype, you need to explicitly convert the dates-as-strings (eg. '08/03/2015'
) into date format.
Something like:
SELECT 'M'||to_char(OD.DATE2,'MM') PERIODE,
count(*) DATA,
OD.DCCPT DCCPT
FROM BDD OD
WHERE BDD = 'phone'
AND OD.SENS = 'Ent'
AND OD.DCCPT IN ('PIOLUC')
AND OD.DATE2 BETWEEN to_date('08/03/2015', 'dd/mm/yyyy') AND to_date('08/03/2016', 'dd/mm/yyyy')
group by 'M'||to_char(OD.DATE2,'MM'),
OD.DCCPT;
Note how I have removed the to_date
from around OD.DATE2
in the select and group by lists, since it is a very bad idea to use to_date
against something that is already a DATE.
By doing so, you force Oracle to do an implicit conversion to a string, which it will do so by using your NLS_DATE_FORMAT parameter to decide what format to output the string as, before it then tries to convert it back to a date using the format mask you specified, like so:
to_date(to_char(od.date2, '<nls_date_format parameter>'), 'DD/MM/YYYY')
If the two format masks aren't the same, you will run into errors... such as the one that prompted you to post this question!
Looking at the line:
OD.DATE2 BETWEEN '08/03/2015' AND '08/03/2016'
Then '08/03/2015'
and '08/03/2016'
are string literals and are not dates.
Oracle will attempt an implicit conversion from a string literal to a date using your NLS_DATE_FORMAT
session parameter as the format mask and if this does not work it will throw an error.
The simple solution is not to use string literals but use date literals instead:
SELECT 'M'||to_char( DATE2, 'MM' ) PERIODE,
count(*) DATA,
DCCPT
FROM BDD
WHERE BDD = 'phone'
AND SENS = 'Ent'
AND DCCPT IN ( 'PIOLUC' )
AND DATE2 BETWEEN DATE '2015-03-08' AND DATE '2016-03-08'
GROUP BY
'M'||to_char( DATE2, 'MM' ),
DCCPT
But you could also specify the format mask:
OD.DATE2 BETWEEN TO_DATE( '08/03/2015', 'DD/MM/YYYY' ) AND TO_DATE( '08/03/2016', 'DD/MM/YYYY' )