Working with dates in Oracle SQL

后端 未结 3 1512
一向
一向 2021-01-22 11:58

I am very new when it comes to using SQL and what I am attempting to do is select the waterUsage and electrcityUsage using only the month and year and select the waterUsage and

3条回答
  •  佛祖请我去吃肉
    2021-01-22 12:53

    Assuming that BILLINGDATE is a DATE column:

    I recommend avoiding the use of the TO_CHAR or EXTRACT function in a case like this, as they're likely to force a full table scan - perhaps not an issue for small databases and tables, but perhaps very important when querying a large table. Instead I suggest getting used to using a BETWEEN comparison for handling date ranges - something similar to the following:

    SELECT ELECTRICITYUSAGE, WATERUSAGE
      FROM MONTHLYBILL
      WHERE ACCOUNTNUMBER = '211' AND
            BILLING_DATE BETWEEN TO_DATE('01-DEC-2012 00:00:00', 'DD-MON-YYYY HH24:MI:SS')
                             AND TO_DATE('31-DEC-2012 23:59:59', 'DD-MON-YYYY HH24:MI:SS')
    

    It's important to remember that in Oracle a DATE column is actually a timestamp and so there is always a date and a time component to it (accurate only down to seconds, not milli- or micro-seconds), so you always need to take the hours/minutes/seconds into account when dealing with Oracle DATEs.

    Best of luck.

提交回复
热议问题