I have a where clause where I am trying to get a date within a certain range with parameters like so,
(AL.INSERTED_DATE BETWEEN (:begindate) AND (:enddate))
The problem is that I need to get six months before the begin date but I get an error, ORA-00904: "DATEADD": invalid identifier, when I try,
(AL.INSERTED_DATE BETWEEN DATEADD(Month,-6,(:begindate)) AND (:enddate))
Can anybody point me out to what I could be doing wrong?
You're not using SQL Server, you're using Oracle - that's why it's giving you an error in Oracle format.
http://psoug.org/definition/ADD_MONTHS.htm
ADD_MONTHS would probably be the best equivalent to what you're trying to do here -
(AL.INSERTED_DATE BETWEEN ADD_MONTHS((:begindate),-6) AND (:enddate))
Check this solution:
Oracle (10g) equivalent of DATEADD(weekday, -3, GETDATE())
Related
i'm stuck with this part of the query, where I need to get only just month and year from my table.
To put in context, i have tried with "TRUNC_DATE", and Oracle give me back an error like
ORA-00904: "DATE_TRUNC": invalid identifier sql
This piece of query get it from Udacity, but in sql Developer looks like it doesn't work.
When I try search deeper, I find something like convert(char(7), ia.invoice_date(), 120) yearMonth, It still not working and an error came back. ORA-00936: missing expression
I tried a lot of ways but no solutions.
If anyones have an idea o something that could help, I will be grateful.
Here below I paste the fragment of the query for guide help:
SELECT
COUNT(ia.invoice_id) total_de_facturas,
SUM(ia.invoice_amount) monto_total,
convert(char(7), ia.invoice_date(), 120) yearMonth
FROM AP.ap_invoices_all ia
GROUP BY ia.vendor_id;
You ae mixing Oracle syntax with Postgres (date_trunc()) and SQL Server (convert()). Many date functions are vendor-specific, so you learn the relevant syntax for your database.
In Oracle, you would use trunc() to truncate a date to the first day of the month:
trunc(ia.invoice_date, 'mm')
Use the EXTRACT function, try these out to test how it works and use it in your query -
SELECT EXTRACT(YEAR FROM DATE '2020-03-07') FROM DUAL;
SELECT EXTRACT(MONTH FROM DATE '2020-03-07') FROM DUAL;
If you want a string value in YYYY-MM format - which seems to be your intention from the 120 style and the char(7) in the SQL Server-syntax convert() call - then you can just use to_char() with that format model:
to_char(ia.invoice_date, 'YYYY-MM') as yearMonth
You don't need to truncate to the first of the month as you're discarding the day part anyway.
if i used like this im not get any error
(ph.x_date >= to_date('01/05/2016') and ph.x_date <= to_date('01/04/2020'))
but if range is big like this
(ph.x_date >= to_date('01/05/2002') and ph.x_date <= to_date('01/04/2020'))
i get invalid number error.
where is my mistake. Thanks
To_date() has to be always used when you know the date data would come in a particular format always that you know before hand. Use to_date like this To_date('date', <dateformat>) this reads the date in the explicit format that you may want to make it read.
In your case it reads default but what if your order of retrieval changes in your x_date i guess your data in xdate column has some issues. Check data before 2016. Different order like mm/yy/dd or etc data different from the to_date format is there
you need to specify the format of date, e.g. to_date('01/05/2002','DD/MM/YYYY') and make sure the date format you get from the table is the same as you specified
I think your data in SELECT list is having an issue
Please check the output of the below command and see whether it returns the output
select *
from data ph
where ph.x_date >= to_date('01/05/2002') and
ph.x_date <=to_date('01/05/2016') ;
I'm trying to find the number of days between two date columns.
I tried to use DATEDIFF but I got an error. What more should I do
Thanks,
Are you sure you are using the correct SQL syntax for your database? Since you're using MySQL, you need to do a
SELECT DATEDIFF('2015-06-05', '2015-08-05');
and the difference is always expressed in days.
On SQL Server you need to specify the unit e.g.
SELECT DATEDIFF(day, '2015-06-05', '2015-08-05');
I'm currently using SSRS to connect to Adempiere (postgresql)
In query, i'm trying to just extract the Month-Year data from the Date so i can use it for Pivot Table/Matrix purposes.
However, when i use this
> SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, adempiere.c_invoice.dateinvoiced, 0),
>
> FROM adempiere.c_invoice
I get an error like this
>SQL Execution Error
>
>Excuted SQL statement: SELECT DATEADD(MONTH,DATEDIFF(MONTH,0,dateinvoiced,0)
>
> FROM adempere.c_invoice
>
>Error Source: PSQLODBC.DLL Error Message:
>
> ERROR [42601] ERROR: syntax error at or near "FROM";
>
>Error while executing query
I've searched high and low and before realising if my problem was a unique problem. Maybe it isnt but i hope someone can help me if im steering in the wrong direction or there's some work around
Thanks alot!
Since you are querying a Postgres database, you obviously cannot use dateadd() which is SQL-Server specific.
Use a Postgres function like:
SELECT date_trunc('month', dateinvoiced) AS month_year1
,to_char(dateinvoiced, 'MM-YYYY') AS month_year2
FROM adempiere.c_invoice
Whatever fits your vague definition Month-Year better.
The first yields a timestamp truncated to month-accuracy. More about date_trunc() in the manual.
The second yields a text of the pattern 06-2013. More about to_char() in the manual.
I have a table named ticket. I am trying to query between two dates, however; I keep getting a syntax error. Any help would appreciated as to what this syntax error may be.
SELECT date
FROM ticket
WHERE date BETWEEN YEAR(ticket.date)=2011 AND MONTH(ticket.date)=11 AND DAY(ticket.date)=06
AND YEAR(ticket.date)=2011 AND MONTH(ticket.date)=11 AND DAY(ticket.date)=12
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=2011 AND MONTH(ticket.date)=11 AND DAY(ticket.date)=06 AND YEAR(ticket.date)=20' at line 1
date BETWEEN YEAR(ticket.date)=2011
doesn't make any sense. The syntax is
date BETWEEN start_range AND end_range
So presumably something like
date BETWEEN '2011-11-06' AND '2011-11-12'
Or as you have a time component you should avoid use of BETWEEN at all and just use
date >= '2011-11-06' AND date < '2011-11-13'
This is an incorrect BETWEEN clause:
BETWEEN YEAR(ticket.date)=2011
It should look like:
BETWEEN YEAR(ticket.date) AND 2011
I think this we will do the trick:
"SELECT
date
FROM
ticket
WHERE
date BETWEEN '2011-11-06' AND 2011-11-12'"
You shouldn't use BETWEEN like you do. You are making a evaluation in a evaluation your (my)sql doesn't support that.
try this:
SELECT date
FROM ticket
WHERE DAY(ticket.date) BETWEEN 6 AND 12
AND MONTH(ticket.date) = 11
AND YEAR(ticket.date) = 2011
You cannot list multiple values in between like that.
Rewrite the query to:
SELECT date
FROM ticket
WHERE date BETWEEN '2011-11-06' and '2011-11-12'