ORA-01847 error when not using dates in the query - sql

I'm trying to run this query in an Oracle 11g database:
SELECT * FROM BALANCE B
WHERE B.EMPLOY_ID = '0016'
AND B.PROJETCT_ID = '5TM-1305002.01.01.01'
but the following error is displayed:
ORA 01847 : day of the month must be between 1 and last day of the month
The EMPLOY_ID field is varchar2(4) and the PROJECT_ID is a varchar2(20).
I do not understand why the Oracle database is trying to convert the parameter values to date values. What is going on?

What happens if you try this?
SELECT * FROM BALANCE B
WHERE TO_CHAR(B.EMPLOY_ID) = '0016'
AND TO_CHAR(B.PROJETCT_ID) = '5TM-1305002.01.01.01'

It could be due to invalid data in one of date columns in table.
You can check your query with rownum condition, just to make sure that the where clause is correct or not.
If it is working then there is no because of B.EMPLOY_ID and B.PROJETCT_ID columns.

Related

Fetching records from SQL based on Month name

So this my table structure and data.
Now I want to filter data based on Month by ExpenseDate column.
So how can I achieve that?
I was trying
select * from tblExpenses where (ExpenseDate = MONTH('April'))
But it throws an error: "Conversion failed when converting date and/or time from character string."
Please help. Thank you.
You are putting month() on the wrong column. It can be applied to ExpensesDate:
select *
from tblExpenses
where month(ExpenseDate) = 4;
Note that month() returns a number, not the name of the month.
I think it is more likely that you want records from a particular April, not every April. This would be expressed as:
where ExpenseDate >= '2018-04-01' and ExpenseDate < '2018-05-01'
I think your where clause is just reversed I think you want this (and change the word to a number)
select * from tblExpenses where Month(ExpenseDate) = 4

Get Data between two dates using Number as Identifier

I am trying to make an SQL query in MS SQL Server where I put an account number to search and it gives me the data between the two date ranges.
code looks like this
select *
from transactions
where accountNo1 = '2005457846' transaction_date between '15-01-2018' and '18-01-2018'
Apparently what am i not doing correctly it tells me
syntax error near transaction_date
You forget to add AND in the query.
select *
from transactions
where accountNo1 = '2005457846' and transaction_date between '15-01-2018' and '18-01-2018'
use " and " also look date format
select * from transactions where accountNo1 ='2005457846' and transaction_date between '15-01-2018' and '18-01-2018'

Sql count and and/or condition in single statement

I am trying to build where clause condition on table having columns “Id”, itemNumber” which can be either 1 or 2 for any row and “date”.
My goal is to write where clause such that i only get “Id’s” where “itemNumber” is 2, and then if count is greater than some value it should filter whole rows to date between today and today+1, otherwise today and today+2.
I tried,
Select Id
from table
where itemNumber=2 And ((count(itemNumber)>2 and date between ‘today’ and ‘today+1’) OR (count(itemNumber)<=2 and date between ‘today’ and ‘today+2’))
I got error saying you need to have sql “having”. Am i doing it wrong?
Try it like this:
SELECT id
FROM t
WHERE itemNumber = 2
GROUP BY id
HAVING (COUNT(itemNumber) > 2 AND date BETWEEN 'today' and 'today+1'))
OR (COUNT(itemNumber) <= 2 AND date BETWEEN 'today' and 'today+2'))
Think of HAVING as a WHERE clause after you have grouped your data, which you have to do if you want to count something by group (or id).

how to fetch rows from table using difference between months (say 14 months) between two dates columns using plsql

I have a table po010 in which columns are polref, POLCRCDTE,PUPDTE.
I have to fetch polref from table po010 where , difference of months between POLCRCDTE and PUPDTE is ateast 14 months.
My tries are .
SELECT polref,POLCRCDTE,PUPDTE FROM po010,
GROUP BY polref
HAVING months_between(pck_utility.f_dtcnv(POLCRCDTE),pck_utility.f_dtcnv(PUPDTE))>14
Note: POLCRCDTE and PUPDTE are number formats ,
and pck_utility.f_dtcnv returns date in DATE format taking number format as input.
Second try is
DECLARE
sysdte DATE;
req_dt DATE;
BEGIN
SELECT system_dt INTO sysdte FROM cs340;
SELECT add_months(sysdte, -14) INTO req_dt FROM po010;
dbms_output.put_line(req_dt);
select * from po010 where pupdte <= pck_utility.f_dtcnv((add_months (sysdte ,-14)));
END;
Note : here taking system_dt from cs340 because in other scenario , i have to compare the dates between those two.
Please help me out.
Both the codes are giving error.
first one error is ORA 00900
second one is giving ORA 06550
.
Basically i need this resultset in a cursor that i need to use in a procedure.
HAVING is used to filter results based on an aggregate, months_between() is not an aggregate function.
Perhaps you just want to use WHERE:
SELECT DISTINCT polref,POLCRCDTE,PUPDTE
FROM po010
WHERE months_between(pck_utility.f_dtcnv(POLCRCDTE)
,pck_utility.f_dtcnv(PUPDTE)
)>14
You typically need to GROUP BY every non-aggregate field in your SELECT list, I'm not sure what you were after with your GROUP BY clause, so I removed it and used DISTINCT in the SELECT list.
Thank you for your support,b it is solved now.
I can use like this.
SELECT DISTINCT p.polref,
p.polcrcdte,
c.system_dt
FROM ddl00.po010 p,
ddl00.cs340 c
WHERE Months_between(pck_utility.F_dtcnv(polcrcdte), system_dt) >= 14

How to get previous months data using KDB query?

How can I retrieve data from the previous month in such a way that, if the query were to be automated, the date value in the query would change accordingly, every month?
So for example:
When query is run on 2012.01.01 --> select * from Table where date >= 2011.12.01
When query is run on 2012.02.01 --> select * from Table where date >= 2012.01.01
When query is run on 2012.03.01 --> select * from Table where date >= 2012.02.01
and so on..
Help would be much appreciated!
I assume you are using Oracle Database;
select * from Table where date >= ADD_MONTHS(TRUNC(SYSDATE),-1)