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.
Related
I have a TABLEA in below format (Updated Columns has Pacific TZ adjusted against UTC)
Status
Updated
Complete
09/Mar/2021 08:27:30AM -1100
Apps Tasks
04/Mar/2021 12:42:12AM -1100
Complete
11/Mar/2021 09:27:30AM -1100
Complete
12/Feb/2021 10:27:30AM -1100
I used below query and I get data as expected but it seems I am messing up date/Timestamp format as I am getting "ORA-01843: not a valid month" in Oracle Apex when I use below query to generate graph.I have been tweaking the query with to_date,to_char,trunc and combinations of TO_TIMESTAMP_TZ and NLS_LANGUAGE but seem to be going in circle.Any inputs would be appreciated if I am missing something in the formatting on the query ?
select TRUNC(TO_TIMESTAMP_TZ(UPDATED),'MONTH') AS Month,COUNT(STATUS) AS "Complete" FROM TABLEA
where STATUS='Complete'
group by TRUNC(TO_TIMESTAMP_TZ(UPDATED), 'MONTH')
order by TRUNC(TO_TIMESTAMP_TZ(UPDATED), 'MONTH')
MONTH
Complete
02/01/2021
1
03/01/2021
2
Thanks
First, you should fix the data model to store date/time values using the appropriate time stamp which is not a string.
Second, I would return the results using dates:
select trunc(to_date(substr(update, 1, 11), 'DD/MMM/YYYY'), 'MON') as yyyymm,
count(*)
from t
group by trunc(to_date(substr(update, 1, 11), 'DD/MMM/YYYY'), 'MON');
You can, of course, format the first column however you want using to_char(). However, I want to emphasize that your data and your code should be using dates or timestamps.
I have the table ORDERS and while trying to find the processing time for each order by subtracting the date of receipt from the date of delivery, I keep getting this error:
SQL0206N "DAY" is not valid in the context where it is used.
SQLSTATE=42703
My query is the following:
SELECT DATEDIFF(DAY, ReceiptDate, DeliveryDate) FROM ORDERS
I suspect it has something to do with the date's format in the columns ReceiptDate and DeliveryDate: DD/MM/YY. Does this have to be converted? What am I doing wrong?
EDIT: Is there an alternative for using DATEDIFF? Can I convert each individual date to an int of days and then subtract the two ints?
If you are using DB2, then there is no DATEDIFF function, which is specific to SQL Server. However, we can easily simulate it by taking a difference of days, using the DAYS() function:
SELECT DAYS(DeliveryDate) - DAYS(ReceiptDate) AS days_diff
FROM ORDERS;
I'm trying to remove the hours, minutes and seconds from a date using the following code :
TRUNC(column_date, 'YY')
But I get this : 01JAN2008:00:00:00, while I want this : 01JAN2008.
What should I do?
TRUNC() works as expected, and returns the original date truncated to year, as a date datatype. You want to format this date before displaying it (this actually means converting it to a string). For this, you can use the TO_CHAR() function.
You probably want:
TO_CHAR(TRUNC(column_date, 'YY'), 'ddmonyyyy')
Please note that this expression could be simplified to avoid the use of TRUNC(), as follows:
'01JAN' || TO_CHAR(column_date, 'yyyy')
I think you want to_char: https://docs.oracle.com/database/121/SQLRF/functions216.htm#SQLRF06129
Try using it in this way:
SELECT TO_CHAR(column_date, 'DD/MON/YYYY')
FROM x;
Where x is the table you are trying to query.
you only need to use
select to_char(column_date,'ddMONyyyy') FROM yourTable
Even you can invoke with
select to_char(column_date,'ddMONyyyy','nls_date_language=english') FROM yourTable
to guarantee to see the abbreviation JAN in the case your session/system date language value is different than english
to display as expected.
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())
For Some reason this query doesn't work in SSIS
Select
IDLOGARCHIVOS,
NOMBREARCHIVO,
FECHACREACION
from
ATEN_TDCMARK.LOGARCHIVOS
where
trunc(FECHACREACION, 'DDD') = trunc(sysdate, 'DDD')
But when I tested in Toad works great what am I doing wrong? Is there a different way to treat dates? And I know that the problem it's related with the comparison of dates because when I remove this condition the query returns values
This works in Toad? I would have expected it to fail due to to_date(sysdate, 'dd/mm/yyyy'). You're trying to convert a date into a date, which Oracle normally raises an error on.
What you seem to want to do is truncate the date to include year, month and day only, i.e. no time component.
This would be trunc(sysdate, 'DD'). trunc(sysdate) would return the same value as 'DD' and it's synonyms are the default, but it's nice to be explicit.
Are you doing the same with your column FECHACREACION? If not and it's a character can I recommend that you store it as a date?
for system date you have to use GETDATE() function in ssis in place of sysdate..
for more information
First To solve my problem I need to modify NS_LANG in oracle registry on my machine locate at (HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOME0). Then I restart Machine and base on the answer of #Ben I modified my query and works in the etl OF SSIS
Select
IDLOGARCHIVOS,
NOMBREARCHIVO,
FECHACREACION
from
ATEN_TDCMARK.LOGARCHIVOS
where
trunc(FECHACREACION, 'DDD') = trunc(sysdate, 'DDD')