简体   繁体   English

我需要从 dateColumn 获取月份和年份

[英]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.

相关问题 从日期列中获取工作日 - Get the weekday from a datecolumn 我需要从结构为 yyyyMMddHHmmss 20170227141500 的时间戳中按年和月分组 - I need to group by year and month from a timestamp structured as yyyyMMddHHmmss 20170227141500 从SQL获取年份和月份 - Get year and month from SQL 如何从表格中获取基于月份和年份的记录? - How can I get records based on month and year from table? 如何从Varchar类型的SQL中获取月份和年份 - How to i get Month & Year from SQL in Varchar Type 如何使用GETDATE()获取过去一个月的月份和年份? - How can I use GETDATE() to get month and year from the past month? 我怎样才能得到从当天,当月或当年的第一天到当天的一周,一个月或一年的日期? - How could I get the the date of the week, month or year starting from the first day up to the current day of the current week, month or year? 我需要 Mysql Query 来查找所选月份和年份的可用日期,不包括从表中获取的日期 - I need Mysql Query to Find available dates for the selected month and year excluding the dates taken from a table 我需要从五月三月到六月的每年月份的数据 - I need data in the order of month in year from March april may and then june 如何获得月份和年份的天数 - How to get number of days from month and year
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM