[英]I need to get month and year from dateColumn
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.为了说明上下文,我尝试使用“TRUNC_DATE”,Oracle 给我返回类似 ORA-00904 的错误:“DATE_TRUNC”:无效标识符 sql 这条查询是从 Udacity 获取的,但在 sql 中,开发人员看起来好像没有不工作。
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.当我尝试更深入地搜索时,我发现类似 convert(char(7), ia.invoice_date(), 120) yearMonth 的内容,它仍然无法正常工作并且返回错误。 ORA-00936: missing expression ORA-00936: 缺少表达式
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; FROM AP.ap_invoices_all ia GROUP BY ia.vendor_id;
You ae mixing Oracle syntax with Postgres ( date_trunc()
) and SQL Server ( convert()
).您将 Oracle 语法与 Postgres ( date_trunc()
) 和 SQL 服务器 ( 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:在 Oracle 中,您将使用trunc()
将日期截断为该月的第一天:
trunc(ia.invoice_date, 'mm')
Use the EXTRACT function, try these out to test how it works and use it in your query -使用 EXTRACT function,尝试这些来测试它是如何工作的,并在您的查询中使用它 -
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:如果您想要一个 YYYY-MM 格式的字符串值——这似乎是 120 样式和 SQL 服务器语法convert()
调用中的char(7)
的意图——那么您可以使用该格式的to_char()
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.您不需要截断到本月的第一天,因为无论如何您都会丢弃一天的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.