im trying to use & in a where condition, but this condition is a date, i want to ask for the month and i dont know how to do that - sql

select cod_prestamo, cod_ejemplar, cod_libro,
trunc (extract (month from fec_prestamo)/3) as "trimestre"
**where fec_prestamo &????**
from prestamo;
This is the code, I don't know how to ask for the month.
I tried with this but it obviously doesn´t work:
select cod_prestamo, cod_ejemplar, cod_libro,
trunc (extract (month from fec_prestamo)/3) as "trimestre"
where fec_prestamo like '/&month/ '
from prestamo;
I need to answer with a month and then Oracle show the books that are registered in that month.

Assuming that fec_prestamo is a DATE data type and that you are trying to use a substitution variable to pass in a month name and want to return three columns plus which quarter contains fec_prestamo then you can use:
SELECT cod_prestamo,
cod_ejemplar,
cod_libro,
TO_CHAR(fec_prestamo, 'Q') as "trimestre"
FROM prestamo
WHERE TO_CHAR(fec_prestamo, 'fmMonth') = &month;
Then when you are prompted for the substitution variable you can enter: 'January' (including the surrounding quotes).

Related

Display By month using select statement

SELECT SUM(Total_A ) FROM Materials_List
This is the snippet of code that I have.
I need it to calculate by month and display by month using SQL.
I also would like it to be a code I can use for any month in the year not just one month at a time.
You seem to be looking for simple aggregation:
select
year(materials_datetime) yr,
month(materials_datetime) mn,
sum(total_a) sum_total_a
from materials_list
group by
year(materials_datetime),
month(materials_datetime)
order by yr, mn
This assumes that column materials_datetime contains the date/time that you want to use to aggregate the data.

SQL Select DATES and assign accordingly

I'm using SQL Oracle;
I want to do the following operation, if the YEAR on the column is equal to my current year then I want to assign the value of 0 to a new column called ano, and if it's a year ahead than current year then I want it to be 1 and so on, if it's less than the current year then I want it to be -1 and so on.
The column name that contains the date is KALW_DATE
SELECT *,
CASE
WHEN YEAR(KALW_DATE) > YEAR(NOW()) THEN (YEAR(KALW_DATE) - YEAR(NOW()))
WHEN YEAR(KALW_DATE) < YEAR(NOW()) THEN (YEAR(KALW_DATE) + YEAR(NOW())
ELSE 0
END as ano
FROM tablename;
but I get an error saying "keyword FROM not found where expected" also I'm connecting to DB2, if that changes anything.
You seem to just want:
SELECT (YEAR(KALW_DATE) - YEAR(NOW())) as ano
As for your code, the parentheses do not balance in the second THEN clause.
Oracle does not have a YEAR() function (at least not the version I'm running). You have to use the EXTRACT() function, like this:
SELECT *,
(extract(year from KALW_DATE) - extract(year from current_date)) as ano
FROM tablename;

View data by date after Format 'mmyy'

I'm trying to answer questions like, how many POs per month do we have? Or, how many lines are there in every PO by month, etc. The original PO dates are all formatted #1/1/2013#. So my first step was to Format each PO record date into 'mmyy' so I could group and COUNT them.
This worked well but, now I cannot view the data by date... For example, I cannot ask 'How many POs after December did we get?' I think this is because SQL does not recognize mm/yy as a comparable date.
Any ideas how I could restructure this?
There are 2 queries I wrote. This is the query to format the dates. This is also the query I was trying to add the date filter to (ex: >#3/14#)
SELECT qryALL_PO.POLN, Format([PO CREATE DATE],"mm/yy") AS [Date]
FROM qryALL_PO
GROUP BY qryALL_PO.POLN, Format([PO CREATE DATE],"mm/yy");
My group and counting query is:
SELECT qryALL_PO.POLN, Sum(qryALL_PO.[LINE QUANTITY]) AS SUM_QTY_PO
FROM qryALL_PO
GROUP BY qryALL_PO.POLN;
You can still count and group dates, as long as you have a way to determine the part of the date you are looking for.
In Access you can use year and month for example to get the year and month part of the date:
select year(mydate)
, month(mydate)
, count(*)
from tableX
group
by year(mydate)
, month(mydate)
You can format it 'YYYY-MM' , and then use '>' for 'after' clause

Show data that is only from a specific quarter

I need to show the data that is only from the 2nd quarter of any year. I am trying to use to_date in the where clause but it is giving me an error. Not sure what I am doing wrong. (Using Oracle 10g)
Code:
SELECT product_name, name
FROM a_product p JOIN a_item i
ON p.product_id=i.product_id
JOIN a_sales_order so ON i.order_id=so.order_id
JOIN a_customer c ON so.customer_id=c.customer_id
WHERE regexp_like(product_name, 'Transducer') AND order_date=to_date(2, 'Q')
The AND portion is where I am having trouble, it is giving me a "format code cannot appear in date input format" order_date is the field within the table that I use to find out what sales were done in quarter 2 of any year.
You have the arguments backwards. You want that to be:
AND to_char(order_date, 'Q') = '2'
The function to_char() (in this context) takes a date and formats it as a string. What string? A string with the quarter in it. You then want to compare it to the value you care about.

can I use the to_char function in where clause in a query with PL/SQL?

I am trying to find out average of electricity volume of certain day in a week with the following query:
SELECT avg(volume)
FROM v_nem_rm16
WHERE to_char(day, 'day') = 'monday';
where the v_nem_rm16 is a table and volume, day are its columns and my query is returning null whatever I change the day value 'monday', 'tuesday',....
is this query wrong?
Actually 'DAY' is returned with padding spaces on the right side.
If you use 'RTRIM' then you can avoid the null values.
SELECT avg(volume)
FROM v_nem_rm16
WHERE RTRIM(to_char(day, 'day')) = 'monday';
I would rather use different date format to_char DAY is nls-dependent that is bad (for instance your software will fail in Spain). D returns number so in your case the query should look like
SELECT avg(volume)
FROM v_nem_rm16
WHERE RTRIM(to_char(day, 'd')) = 1;