Literal does not match format string in date duration in oracle - 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')

Related

Why does the date only shows up but not the time in Oracle?

I've inserted a record into reserve table like this:
INSERT INTO reserve(garage_code, vunit_id, reserve_date_time_placed, renter_no)
SELECT *
FROM (SELECT vehicle_unit.garage_code, vehicle_unit.vunit_id
FROM vehicle_unit
INNER JOIN garage ON vehicle_unit.garage_code=garage.garage_code
WHERE vehicle_insurance_id ='sports-ute-449-12b' AND garage_name= 'Melbourne Central VIC'),
(SELECT TO_DATE('2019/05/04 04:00 PM', 'yyyy/mm/dd hh:mi AM') FROM dual),
(SELECT renter_no FROM RENTER WHERE renter_fname='Van' AND renter_lname='DIESEL');
However, only the specified dates shows up but not the time. I tried
(SELECT TO_CHAR(TO_DATE('2019/05/04 04:00 PM', 'yyyy/mm/dd hh:mi AM'), 'yyyy/mm/dd hh:mi AM') FROM dual)
But it returns error 'invalid string literal'?

Re ORA-01843: not a valid month error

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

Excluding rows from outer join

I am trying to extract rows from a table to gain the start and end time for events, these are held in a one to many table relationship so that for each instanceid in the master table you can have a number of entryids in the child. I have successfully written the below query that extracts the required data converting epoch timestamps, but am getting rows with NULL values. I can see that to exclude these I need to only select rows from table_a where the status value reflect 1 or 6, but am not sure of syntax for achieving this, I have tried combos of bold highlighted line below to no avail.
SELECT a.summary
to_char(date '1970-01-01' + b.create_date/86400, 'DD Mon YYYY HH24:MI:SS') as start_date,
to_char(date '1970-01-01' + c.create_date/86400, 'DD Mon YYYY HH24:MI:SS') as completed_date,
to_char (TRUNC (SYSDATE) + NUMTODSINTERVAL ((c.create_date - b.create_date), 'second'), 'hh24:mi:ss') as Elapse_Time,
a.status
FROM o2_hpov_casecreation a
LEFT OUTER JOIN o2_hpov_casecreation_audit b
ON (a.instanceid = c.entryid
AND B.action= 'Queueing Simulation phase on SYS:Action')
LEFT OUTER JOIN o2_hpov_casecreation_audit c
ON (a.instanceid = c.entryid
AND c.action = 'Notifications completed')
AND a.status IN (2,6)
You need to add your filter criteria to the where clause
below is a corrected version of your query
SELECT a.summary
to_char(date '1970-01-01' + b.create_date/86400, 'DD Mon YYYY HH24:MI:SS') as start_date,
to_char(date '1970-01-01' + c.create_date/86400, 'DD Mon YYYY HH24:MI:SS') as completed_date,
to_char (TRUNC (SYSDATE) + NUMTODSINTERVAL ((c.create_date - b.create_date), 'second'), 'hh24:mi:ss') as Elapse_Time,
a.status
FROM o2_hpov_casecreation a
LEFT OUTER JOIN o2_hpov_casecreation_audit b
ON (a.instanceid = c.entryid
AND B.action= 'Queueing Simulation phase on SYS:Action')
LEFT OUTER JOIN o2_hpov_casecreation_audit c
ON (a.instanceid = c.entryid
AND c.action = 'Notifications completed')
WHERE a.status IN (2,6)

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')

Query transactions that occured only at a certain time

I am trying to run a select query that will pull all meds scheduled at 2300 for a date range. Is there a way I can convert the scheduled date/time to just hour? This is what I have so far:
SELECT DISTINCT USERCODE,
TRANSACTIONID,
ACTION,
TRANSACTIONHOUR,
SOURCE,
RXNUMBER,
DESCRIPTION,
MASTERPATIENTID,
FACILITYCODE,
ADMINISTRATIONTIME
FROM ( ABC.TL TL
INNER JOIN
ABC.S_VIEW S_VIEW
ON (TL.RXNUMBER = S_VIEW.RXNUMBER))
INNER JOIN
ABC.PV PATIENTVISIT
ON (TL.MASTERPATIENTID = PV.MASTERPATIENTID)
WHERE (TL.USERCODE NOT IN ('ABC'))
AND (TL.ACTION IN ('A', 'DC'))
AND (TL.TRANSACTIONHOUR BETWEEN to_date('2011-07-01 00:00:00', 'yyyy/mm/dd hh24:mi:ss') AND to_date('2011-09-30 23:59:59', 'yyyy/mm/dd hh24:mi:ss')
I would like the query to include all dispense during the specified dates but only at 2300 time. Database is oracle 10g.
First, you have given the date string in a different format and the format in different format. Make it consistent.
Try this:
SELECT DISTINCT USERCODE,
TRANSACTIONID,
ACTION,
TRANSACTIONHOUR,
SOURCE,
RXNUMBER,
DESCRIPTION,
MASTERPATIENTID,
FACILITYCODE,
ADMINISTRATIONTIME
FROM ( ABC.TL TL
INNER JOIN
ABC.S_VIEW S_VIEW
ON (TL.RXNUMBER = S_VIEW.RXNUMBER))
INNER JOIN
ABC.PV PATIENTVISIT
ON (TL.MASTERPATIENTID = PV.MASTERPATIENTID)
WHERE (TL.USERCODE NOT IN ('ABC'))
AND (TL.ACTION IN ('A', 'DC'))
AND (TL.TRANSACTIONHOUR BETWEEN to_date('2011/07/01 00:00:00', 'yyyy/mm/dd hh24:mi:ss') AND to_date('2011/09/30 23:59:59', 'yyyy/mm/dd hh24:mi:ss')
AND TO_CHAR(TL.TRANSACTIONHOUR, 'HH24MI') = '2300' --THIS IS THE NEW CONDITION

Resources