Working with dates in Oracle SQL

后端 未结 3 1475
一向
一向 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 13:03

    One option uses TO_CHAR:

    select electrcityUsage, waterUsage 
    from monthlyBill
    where accountNumber = '211' and
         to_char(billing_date, 'MM-YYYY') = '12-2012'
    

    This assumes that you're actually using Oracle, and not SQL Server.

    If you wanted 2012 and 2011 then just go ahead and add another condition to the WHERE clause. I might use EXTRACT in this case:

    select electrcityUsage, waterUsage 
    from monthlyBill
    where accountNumber = '211' and
        extract(month from billingDate) = 12 and
        extract(year from billingdate) in (2011, 2012)
    

提交回复
热议问题