I have an oracle table that store transaction and a date column. If I need to select records for one year say 2013 I do Like this:
select *
from sales_table
where tran_date >= '01-JAN-2013'
and tran_date <= '31-DEC-2013'
But I need a Straight-forward way of selecting records for one year say pass the Parameter '2013' from an Application to get results from records in that one year without giving a range. Is this Possible?
Use the extract function to pull the year from the date:
select * from sales_table
where extract(YEAR from tran_date) = 2013
You can use to_date function
http://psoug.org/reference/date_func.html
select *
from sales_table
where tran_date >= to_date('1.1.' || 2013, 'DD.MM.YYYY') and
tran_date < to_date('1.1.' || (2013 + 1), 'DD.MM.YYYY')
solution with explicit comparisons (tran_date >= ... and tran_date < ...) is able to use index(es) on tran_date field.
Think on borders: e.g. if tran_date = '31.12.2013 18:24:45.155' than your code tran_date <='31-DEC-2013' will miss it
select last_name,hire_date
from employees
where extract(year from hire_date) = 2006;
select FIRST_NAME , to_char(hire_date, 'YYYY') YR FROM employees where to_char(hire_date, 'YYYY')= '2006'
select * from table_name where YEAR(date_column_name) = 2019
Related
I came across a problem that in selecting the date for current desired month and year. I tried the 2 statements shown below but failed to execute the query
select to_char(sysdate, 'Month') from income
select * from income where to_char(sysdate,month) = 'feb'
Update
But after researching and learning more in depth on oracle docs website. What i came out with is to use "between" clause. Specifying the first day and last day of the month . Doing so, it will execute the desired month/year
For an example
SELECT column_name
FROM table_name where column_name = (Your own value) AND
column_date >= to_date('01/02/2012', 'dd/mm/yyyy')
and column_date < to_date('01/03/2012', 'dd/mm/yyyy')
I hope this help :)
Are you after something like:
select *
from income
where <date_column> >= to_date('01/05/2019', 'dd/mm/yyyy')
and <date_column> < to_date('01/06/2019', 'dd/mm/yyyy')
(replacing <date_column> with the name of the date column in your income table that you want to filter on)?
I think you can use the following query:
select *
from income
where to_char(<date_column>,'MON-RRRR') = 'MAY-2019';
If you want to pass in a string like 'May 2012', then I would recommend:
select i.*
from income i
where i.datecol >= to_date('May 2012', 'Mon YYYY') and
i.datecol < to_date('May 2012', 'Mon YYYY') + interval '1' month;
That said, I think your application should turn the string value into a date range and you should use that range in your query:
select i.*
from income i
where i.datecol >= :datestart
i.datecol < :dateend + interval '1 day';
I strong encourage you to avoid between with dates, particularly in Oracle. The date data type has a built-in time component, and that can throw off the comparisons.
I am trying to create a code to join two statements each from different table and conditions, as follows:
the first statement:
select TO_CHAR(Entry_date, 'MON.YYYY') AS Months, count(Customer_id) "Count Customer"
from table1
where entry_date >= TO_DATE('01.01.1900', 'DD.MM.YYYY')
AND entry_date <= TO_DATE('31.12.2017', 'DD.MM.YYYY')
and Customer_status = 'Active'
group by TO_CHAR(entry_date,'MON.YYYY')
order by to_date(TO_CHAR(entry_date, 'MON.YYYY'),'MON.YYYY')
The second statement:
select count (order_id) "Order"
from table2
where leave_date >= TO_DATE('01.01.1900', 'DD.MM.YYYY')
AND leave_date <= TO_DATE('31.12.2017', 'DD.MM.YYYY')
group by TO_CHAR(leave_date,'MON.YYYY')
order by to_date(TO_CHAR(leave_date, 'MON.YYYY'),'MON.YYYY')
the result should look like this
Months Count Customer Order
Jan. 2017 15 0
Feb. 2017 1 8
Mar. 2017 30 10
The order should be dependent on the Months that were stated in the first statement.
Thanks for your help in advance.
I would write this as:
select yyyymm, sum(cust_count) as cust_count, sum(num_orders) as num_orders
from ((select to_char(entry_date, 'YYYY-MM') as yyyymm, count(*) as cust_count, 0 as num_orders
from table1
where entry_date >= date '1900-01-01' and
entry_date < date '2018-01-01' and
Customer_status = 'Active'
group by to_char(entry_date, 'YYYY-MM')
) union all
(select to_char(leave_date, 'YYYY-MM') as yyyymm, 0,
count(*) as num_orders
from table2
where leave_date >= date '1900-01-01' and
leave_date < date '2018-01-31'
group by to_char(leave_date, 'YYYY-MM')
)
) tt
group by yyyymm
order by yyyymm;
Notes on some changes:
The use of date rather than to_char() with date constants.
The use of the format "YYYY-MM", which orders correctly. (You don't have to use it but it recommended.)
The union all brings all the data together. In Oracle, you can also use a full outer join, but that requires more use of coalesce().
Let's say i have selected data from oktober until desember like this
i want to get sum of buying customer in every months (okt-des)
The result i want is like table below
i already know how to get last day of months but i don't have idea query in SQL to get result like i need
One way to do this - if you just need the data for those three months - is to use conditional aggregation:
select name,
sum(case when dt >= date '2017-10-01' and dt < date '2017-11-01'
then buying end) as oktober,
sum(case when dt >= date '2017-11-01' and dt < date '2017-12-01'
then buying end) as november,
sum(case when dt >= date '2017-12-01' and dt < date '2018-01-01'
then buying end) as desember
from YOUR_TABLE
where dt >= date '2017-10-01' and dt < date '2018-01-01'
group by name
;
Note that date is an Oracle keyword which should not be used as a column name; I changed it to dt. YOUR_TABLE should be your actual table name.
Try this
DESC TABLE_NAME
NAME VARCHAR2(20 BYTE)
BUYING NUMBER
BUYING_DATE DATE
select * from
(
select name,buying,RTRIM(to_char(buying_Date,'Month')) dd
from
TABLE_NAME
)
PIVOT
(
SUM(buying)
for dd IN ('October','November','December')
);
How can I get the year and month of a date in the where clause using Oracle.
I used to be working with SQL server and it was as simple as YEAR(FIELDNAME) and MONTH(FIELDNAME).
I have tried the following:
SELECT *
FROM myschema.mytablename
WHERE EXTRACT(YEAR FROM myDATE) = 2017
however it gives ORA-30076 Error
Have you tried EXTRACT()?
SELECT EXTRACT(YEAR FROM DATE '2017-12-01') FROM DUAL;
2017
SELECT EXTRACT(MONTH FROM DATE '2017-12-01') FROM DUAL;
12
I tried this in sql fiddle with 11g and it works in WHERE clause too.
http://sqlfiddle.com/#!4/fb2b09/2
SELECT *
FROM myschema.mytablename
WHERE TO_CHAR(myDATE, 'YYYY') = '2017';
Explicitly convert year part of DATE into CHAR and compare it with literal.
For year and month comparison:
SELECT *
FROM myschema.mytablename
WHERE TO_CHAR(myDATE, 'YYYY') = '2017' AND TO_CHAR(myDate, 'MM') = '07';
Your query should work, but a better alternative would be
SELECT *
FROM YOUR_TABLE
WHERE MY_DATE BETWEEN TO_DATE('01-JAN-2017', 'DD-MON-YYYY')
AND TO_DATE('01-JAN-2018', 'DD-MON-YYYY') - INTERVAL '1' SECOND
Best of luck.
I did this with the function EXTRACT() and it works good for me.
I'm gonna share the query code here:
SELECT extract(YEAR from s.CREATE_DATE) as YEAR, extract(MONTH from s.CREATE_DATE) as MONTH, s.SALES_PERSON_GID, COUNT(s.SALES_ORDER_ID) as "TOTAL ORDERS" FROM SALES_ORDERS s, SALES_ORDER_STATUS ss WHERE s.SALES_ORDER_ID = ss.SALES_ORDER_ID and ss.STATUS_TYPE_ID = 'SALE ORDER STATUS' and ss.STATUS_VALUE_GID = 'SALE ORDER STATUS_DELIVERED' GROUP BY s.SALES_PERSON_GID,s.CREATE_DATE
I have a table that has columns YEAR and MONTH, which are varchars, which have a format like MONTH = '02', YEAR = '2011'.
What query can I use to get the last eight months of data, excluding the current month?
Try
where to_date(year || month, 'YYYYMM')
between add_months(trunc(sysdate, 'MM'), -8) and trunc(sysdate)
Try this:
SELECT *
FROM myTable
WHERE (year, month) IN
(
SELECT TO_CHAR(ADD_MONTHS(SYSDATE, -level), 'RRRR') AS year,
TO_CHAR(ADD_MONTHS(SYSDATE, -level), 'MM') AS month
FROM dual
CONNECT BY LEVEL < 8
)
Another version:
SELECT *
FROM myTable
WHERE year||month IN
(
SELECT TO_CHAR(ADD_MONTHS(SYSDATE, -level), 'RRRRMM') AS yearmonth
FROM dual
CONNECT BY LEVEL < 8
)
Best I can think of is this:
SELECT * FROM table WHERE (MONTH || YEAR) IN ('012011','122010', et cetera)
It is a brute force method, so it is not feasible if you want your query to generalize.