what is the best way to get result using the below query - plsqldeveloper

Following query does not show any output but it does not have any errors -
select inv_id
from invoice_table
where
INVOICE_DATE BETWEEN to_char(P_INVOICE_START_DATE,'DD-MON-YYYY')AND to_char(P_INVOICE_END_DATE,'DD-MON-YYYY');
Where did I go wrong and how can i improve it?

I managed to find solution all parameters are of type Varchar2 and used the FND_DATE.CANONICAL_TO_DATE function as below
`***...AND T.INVOICE_DATE BETWEEN FND_DATE.CANONICAL_TO_DATE(P_INVOICE_START_DATE) AND FND_DATE.CANONICAL_TO_DATE(P_INVOICE_END_DATE);***`

Related

CAST() function MYSQL doesn't seem to work for me? need help please

Hi everyone so the issue I am having is that I am trying to use the CAST() function to change a column where the values are stored as varchars into signed(64 bit integers) so that I can sort the table by the value of the integers in that column. Below I am sharing the code I am using in MYSQL and an image of the table so you can see what it is I am working with. The problem is when I try to sort by emp_id after running the code it still won't sort properly based on the integer value and I am not sure what I am doing wrong. Any help is greatly appreciated.
SHOW databases;
USE ex;
SHOW tables;
SELECT *
FROM string_sample;
SELECT CAST(emp_id AS SIGNED), emp_name,
FROM string_sample;
Problem solved seems to be what was causing the issue was that after my use of CAST(emp_id AS SIGNED) I didn't give it an alias when I changed the line to CAST(emp_id AS SIGNED) AS emp_id the table behaved correctly and converted emp_id to an int which allowed me to sort it by the emp_id values correctly.

Access query amazing

When I do that on access, SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE NOT (RMonturesImp.N°Fac IN (1,2,5))
GROUP BY RMonturesImp.N°Fac;
but when I do this
SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE NOT (RMonturesImp.N°Fac IN Requête2)
GROUP BY RMonturesImp.N°Fac;
it doesn't work (it shows 1,2,5 indeed) although the result of Requête2 (which is a query) is also (1,2,5). I can't understand this!
Thanks in advance
It's quite easy. The IN (1,2,5)) must be explicit as SQL will not evaluate an expression not to say a function to obtain the values for IN.
So build your SQL in code creating the string, or pull the values from a (temp) table.
Try this:
SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE RMonturesImp.N°Fac NOT IN (Select N°Fac From Requête2)
GROUP BY RMonturesImp.N°Fac;

Getting no output while passing String parameters in mysql query

In the given below these two queries the first query runs successfully and display the result but while running second query i am getting no output and no error.
So can any please explain why i am not getting output in second query.
String firstdate="2014-11-01";
String lastdate="2014-11-30";
//1 query
getschedule=con.getreader_schedule("SELECT startTime,endtime,available,comments FROM
reader_available_time WHERE startTime BETWEEN '2014-11-01' AND '2014-11-30' AND
reader_id="+Integer.parseInt("136"));
//2 query
getschedule=con.getreader_schedule("SELECT startTime,endtime,available,comments FROM
reader_available_time WHERE startTime BETWEEN "+firstdate+" AND "+lastdate+" AND
reader_id="+Integer.parseInt("136"));
Well when you doing second query and giving external parameters u need to do something like this.
getschedule=con.getreader_schedule("SELECT startTime,endtime,available,comments FROM
reader_available_time WHERE startTime BETWEEN '"+firstdate+"' AND '"+lastdate+"' AND
reader_id="+Integer.parseInt("136"));
tihs is what i think its should work.
Ideally, you should be using prepared statements in your queries, to avoid this kind of issues. For a tutorial on prepared statements(using java), check here
To solve your existing query,
You are not getting output in the second query because, on substitution of the variables, your query would be:
WHERE STARTTIME BETWEEN 2014-11-01 AND 2014-11-30
which is wrong and bound to throw exceptions.
Correct query should be
WHERE STARTTIME BETWEEN '2014-11-01' AND '2014-11-30'
which implies, you should change your text to
WHERE STARTTIME BETWEEN '"+firstdate+"' AND '"+lastdate+"'

Trying to use BETWEEN condition to return rows based on date but getting no results

I have a database containing a list of events, it is formatted something like this:
Datef Event Location Discipline
10/01/2012 MG Training Brooklands MG
I am running this query in order to get the results between certain dates:
SELECT * FROM events WHERE Discipline IN ('MG') AND Datef BETWEEN 01/01/2012 AND 31/01/2012
The query runs correctly and I know that there are relevant results in the database, but I receive no results when running the query in phpmyadmin (I just get told "Your SQL query has been executed successfully").
I was wondering if anyone had any idea why this wouldn't be returning results?
Update:
Putting dates in quotes (e.g. SELECT * FROM events WHERE Discipline IN ('MG') AND Datef BETWEEN 01/01/2012 AND 31/01/2012) kinda works but there's a bug.
I've certain dates doesn't work. e.g. SELECT * FROM events WHERE Discipline IN ('COMM') AND Datef BETWEEN '2012-02-01' AND '2012-02-29' shows no results, even though there is an event on 2010-02-01. Is this a peculiarity in the BETWEEN algorithm, or am I still getting my query wrong?
Without quotes or anything designating those values as dates, it will simply do the math on the integers you wrote.
In other words, you ask for
BETWEEN 01/01/2012 AND 31/01/2012
So you get 1/1=1, then 1/2012 which is almost 0.
Then you do 31/1=31, then 31/2012 which is also almost 0.
So it becomes
BETWEEN 0 and 0
Try wrapping your code strtotime(). MySQL and PHP use different formatting.
For some additional info, here are a couple of links:
http://www.richardlord.net/blog/dates-in-php-and-mysql
http://www.binarytides.com/blog/parse-and-format-dates-between-php-and-mysql/
Your column Datef is of the wrong type and should be datatime
SELECT
*
FROM
`events`
WHERE
`Discipline` IN ('MG')
AND
`Datef` BETWEEN '2012-01-01' AND '2012-01-31'

How can I resolve this issue using an sql query

This is my initial question....well I got a response but I'm still stuck.
Can anyone please explain how can I achieve this in mysql:
I have two fields in mysql, 'cap_commdate' with DATE TYPE and 'cap_policyterm' with INT TYPE. I want to have another field called 'cap_maturityDate' which will automatically compute the policy term pereod in years (in Layman's expression ie: cap_commdate*cap_policyterm). What is the right SQL query to use; or what is the best approach; and I want to use it in my recordset to prepare a confirmation page... please a simple explanation...
I have tried the following:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) ...
I ran the query and got errors; so i edited it and used:
SELECT
DATE_ADD("cap_commdate", INTERVAL "cap_policyterm" YEAR) AS cap_maturity FROM capital
All I got was empty fields. Please help out.
Remove the quotes around the field names
SELECT
DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) AS cap_maturity FROM capital
I think this should work.
I know its is an old question, but just trying to help others