• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

How can I format my date properly inthis query?

Hi There,

I ahve the below query with 3 parameters. How can I properly format the dates? Right now i get the error ORA-01841: (full) year must be between -4713 and +9999, and not be 0?

SELECT     primary_mobile_id AS Primary_Mobile_Num, NVL(b.description, 'Not-rated') AS billable, mobile_id AS Originator, 
                      cntry_num || called_num AS Destination, orig_zone AS Ocean_Region, to_char(call_dt, 'DD/MM/YYYY HH24:MI:SS') AS call_dt, call_dt AS call_date, 
                      customer_code AS CustCode, NVL(SUM(minutes), 0.00) AS Units, NVL(b_uom, 'mins') AS UOM, acct_period AS AcctPeriod, SUM(NVL(total_charge, 0)) 
                      AS Charges, rate_currency AS Currency
FROM         cdr_customer c, billable b
WHERE     primary_mobile_id = :MOBILE1 AND call_dt BETWEEN to_date(':SDATE1' || ' 00:00:00', 'YYYY-MM-DD HH24:MI:SS')  AND to_date(':EDATE1' || ' 23:59:59', 'YYYY-MM-DD HH24:MI:SS') AND c.billable = b.billable (+) AND rev_num >= 0
GROUP BY primary_mobile_id, mobile_id, cntry_num || called_num, orig_zone, to_char(call_dt, 'DD/MM/YYYY HH24:MI:SS'), call_dt, customer_code, 
                      NVL(b_uom, 'mins'), acct_period, invoice_num, rate_currency, NVL(b.description, 'Not-rated')
SELECT     imn_number AS Primary_Mobile_Num, NVL(b.description, 'Not-rated') AS billable, from_user AS Originator, to_user AS Destination, NVL(from_site, 
                      to_site) AS Ocean_Region, to_char(date_sent, 'DD/MM/YYYY HH24:MI:SS') AS call_dt, date_sent AS call_date, customer_code AS CustCode, 
                      NVL(SUM(billable_units), 0.00) AS Units, billable_uom AS UOM, acct_year AS AcctPeriod, SUM(NVL(space_charge, 0) + NVL(landline_charge, 0) 
                      + NVL(p_ndn_charge, 0)) AS Charges, rate_currency AS Currency
FROM         c_msgs c, billable b
WHERE     imn_number = :MOBILE1 AND date_sent BETWEEN :SDATE1 AND :EDATE1 AND c.billable = b.billable (+) AND rev_num >= 0
GROUP BY imn_number, from_user, to_user, NVL(from_site, to_site), to_char(date_sent, 'DD/MM/YYYY HH24:MI:SS'), date_sent, customer_code, billable_uom, 
                      acct_year, rate_currency, NVL(b.description, 'Not-rated')

Open in new window

Rhonda Carroll
Rhonda Carroll
  • 2
1 Solution
are :sdate1 and :edate1  dates or are they strings?

in one query you convert them to dates

  AND call_dt BETWEEN TO_DATE(':SDATE1' || ' 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
                     AND  TO_DATE(':EDATE1' || ' 23:59:59', 'YYYY-MM-DD HH24:MI:SS')

but in the other you don't

AND date_sent BETWEEN :sdate1 AND :edate1

are call_dt and date_sent both dates?
in either case you don't want to put the binds inside the quotes

':SDATE1'  ':EDATE1'

if they are dates you don't want to convert them at all
if they are strings you should convert them the same in both cases

BETWEEN TO_DATE(:SDATE1 || ' 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
                     AND  TO_DATE(:EDATE1 || ' 23:59:59', 'YYYY-MM-DD HH24:MI:SS')

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now