Re ORA-01843: not a valid month error - oracle

Query:
SELECT t3.e_id, t6.ei_id
FROM t3, t6
WHERE
(TO_CHAR(t6.EI_Q17, 'YYYYMMDD') BETWEEN 20160801 AND 20170731)
AND (t3.E_STATUS = 'W')
AND (t3.E_START < '1/8/2016')
AND (t3.E_END > '31/7/2017')
but I get an error: ORA-01843: not a valid month.
Any idea how this can be resolved?
Thanks,
Ar

Use standard date syntax and explicit joins:
SELECT t3.e_id, t6.ei_id
FROM t3 CROSS JOIN t6
WHERE t6.EI_Q17 BETWEEN DATE '2016-08-01' AND DATE '2017-07-31' AND
t3.E_STATUS = 'W' AND
t3.E_START < DATE '2016-08-01' AND
t3.E_END > DATE '2017-07-31';
If you use DATE and ISO 8601 standard date formats, then you don't have to worry about the localization settings for date constants. You also don't need to clutter up your queries with function calls.

Firstly you must follow Gordon suggestion to Use standard date syntax and explicit joins. Secondly from your posted query it looks like the column t6.EI_Q17 is varchar having date. In this case you need to cast it twice like below:
SELECT t3.e_id, t6.ei_id
FROM t3 CROSS JOIN t6
WHERE TO_CHAR (TO_DATE (t6.EI_Q17, 'YYYY-MM-DD'), 'YYYYMMDD') BETWEEN 20160801
AND 20170731
AND t3.E_STATUS = 'W'
AND t3.E_START < TO_DATE ('2016-08-01', 'YYYY-MM-DD')
AND t3.E_END > TO_DATE ('2017-07-31', 'YYYY-MM-DD')
Simple demo:
SQL> select dt
from
(select to_char(to_date('2017-06-01','YYYY-MM-DD'),'YYYYMMDD') dt from dual )
where dt between 20170601 and 20170603 ;
Output:
20170601

Related

encountered the symbol FROM when expecting one of the following pl sql

I am trying to extract year from datefield but when I use extract (year from
datefield) I get this error
Encountered the symbol FROM when expecting one of the following pl sql
cursor o1 is
select substr(tarifa,1,2), count(*)
from pol p, uvod u, doppov d
where extract(year FROM datum_dop) = EXTRACT(YEAR FROM sysdate)
and izdavanje >='1-jul-13'
and p.orgjed = u.sorgz (+)
and DATUM_PREKIDA is not null
and p.polica=d.polica and d.pov_dopl='P'
and d.status='F'
and cisti_ao(p.polica)!=0
group by substr(tarifa,1,2);
Where did I made mistake ?
Ah, this is Forms, probably 6i.
Its engine doesn't know extract function. Change that line to
where to_char(datum_dop, 'yyyy') = to_char(sysdate, 'yyyy')
This would, though, make index on datum_dop column (if it exists) unusable and force Oracle to convert dates to strings, so you'd rather try with
where datum_dop >= trunc(sysdate, 'yyyy')
and datum_dop < add_months(trunc(sysdate, 'yyyy'), 12)
Other than that:
count(*) should have an alias (if you plan to use it), e.g. count(*) as broj_tarifa
if izdavanje is date, don't compare it to a string ('1-jul-13') but date, e.g. izdavanje >= to_date('01.07.2013', 'dd.mm.yyyy')
use table aliases for all columns

Convert Numeric format dates to dates in Oracle

This is how my Date column looks like
RPT_DT (Data Type is Number)
20180131
20180130
20180129
I wanna extract month out of these dates(either Month or mm), and I tried below
select extract(month from to_date(Rpt_dt))
from
(
select distinct to_char(to_date(RPT_DT,'yyyymmdd'),'mm/dd/yyyy') Rpt_dt
from TABLE_NAME
)
I am getting the error "Not a valid month"
if there is not any particular reason to have a double conversion I would suggest you to handle the problem with this simple query:
select substr(to_char(RPT_DT),5,2)from THE_TABLE
this query should be more performant since it make only one conversion. in your sample you transform:
a number to a date
then a date to a char
the char again in date
finally you extract the month
let me know if it help
r.
try this,
SELECT EXTRACT(MONTH FROM TO_DATE(rpt_dt, 'YYYYMMDD'))
FROM TABLE_NAME;
and I believe you need to modify your query as you did not put the format 'MM/DD/YYYY',
select extract(month from to_date(Rpt_dt, 'MM/DD/YYYY'))
from
(
select distinct to_char(to_date(RPT_DT,'yyyymmdd'),'mm/dd/yyyy') Rpt_dt
from TABLE_NAME
)
This back-and-forth conversion is useless. Try this
select
extract(month from to_date(RPT_DT,'yyyymmdd'))
from TABLE_NAME;

Oracle query not giving result for current_date

What is the query in Oracle to fetch the data for current_date
the column end_date is like the following
end_date
27-10-16 03:35:00.000000000 PM
23-11-16 11:15:00.000000000 AM
02-11-16 03:00:00.000000000 PM
08-11-16 09:00:00.000000000 AM
Like I am running the following query as
Select * from table1
where end_date < TO_DATE('2017-04-11 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
it is running successfully, but when i replace the query with the current date ... it is not giving the results
Select * from table1
where end_date < TO_DATE(current_date, 'YYYY-MM-DD HH24:MI:SS')
could someone tell me what is the cause the second query is not giving results.
CURRENT_DATE returns date. There is no need to use TO_DATE. The below query should be enough.
Select * from table1
where end_date < current_date;
If you run the below query you'll understand what went wrong for you. Year becomes 0011.
SELECT TO_DATE(current_date, 'YYYY-MM-DD HH24:MI:SS') FROM DUAL;
Please note that CURRENT_DATE returns the current date in the session time zone. SYSDATE returns the current date and time set for the operating system on which the database resides. This means that CURRENT_DATE and SYSDATE can return different results. You can have a look at this
The query worked like this :
Select * from table1
where trunc(end_date) < trunc(sysdate)
Trunc is used to compare the both dates and it fetch the results.
CURRENT_DATE is already a DATE value. You can format the output using to_char if you want.
end_date < CURRENT_DATE should do the job. Or you can set the nls parameter accordingly for a better readability.
If you are comparing only date, without timestamp, you can go with trunc()

Literal does not match format string in date duration in oracle

i am facing problem kindly help me my code is
SELECT 'NULL' AS sender_mobile,
ci.str_identification AS receiver_mobile,
TO_CHAR (cc.dat_creation, 'YYYYMMDD') AS datee,
TO_CHAR (cc.dat_creation, 'hh24:Mi:ss') AS timee
FROM pbxhbl.agent_details ad
INNER JOIN pbxmob.customers_identifications ci
ON ad.id_customer = ci.id_customer
INNER JOIN pbxmob.customers_credentials cc
ON ci.id_customer = cc.id_customer
INNER JOIN pbxmob.credential_types ct
ON cc.id_credential_type = ct.id_credential_type
WHERE cc.dat_creation BETWEEN '21-August-2015 06:00:00 PM'
AND '21-August-2015 06:59:59 PM';
You'll be better off if you are explicit about your date format. Here is an example that does the same thing as your query:
where cc.DAT_CREATION
between to_date('2015-08-21 18:00:00', 'YYYY-MM-DD HH24:MI:SS')
and to_date('2015-08-21 18:59:59', 'YYYY-MM-DD HH24:MI:SS')

Oracle comparing timestamp with date

I have a timestamp field and I just want to compare the date part of it in my query in Oracle
How do I do that,
SELECT *
FROM Table1
WHERE date(field1) = '2012-01-01'
You can truncate the date part:
select * from table1 where trunc(field1) = to_date('2012-01-01', 'YYYY-MM-DD')
The trouble with this approach is that any index on field1 wouldn't be used due to the function call.
Alternatively (and more index friendly)
select * from table1
where field1 >= to_timestamp('2012-01-01', 'YYYY-MM-DD')
and field1 < to_timestamp('2012-01-02', 'YYYY-MM-DD')
You can truncate the date
SELECT *
FROM Table1
WHERE trunc(field1) = to_Date('2012-01-01','YYY-MM-DD')
Look at the SQL Fiddle for more examples.
to_date format worked for me. Please consider the date formats:
MON-, MM, ., -.
t.start_date >= to_date('14.11.2016 04:01:39', 'DD.MM.YYYY HH24:MI:SS')
t.start_date <=to_date('14.11.2016 04:10:07', 'DD.MM.YYYY HH24:MI:SS')

Resources