Showing Month-Year from Date, in Query for SSRS - sql

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.

Related

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.
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.

Cannot use calculated offset in BigQuery's DATE_ADD function

I'm trying to create a custom query in Tableau to use on Google's BigQuery. The goal is to have an offset parameter in Tableau that changes the offsets used in a date based WHERE clause.
In Tableau it would look like this:
SELECT
DATE_ADD(UTC_USEC_TO_MONTH(CURRENT_DATE()),<Parameters.Offset>-1,"MONTH") as month_index,
COUNT(DISTINCT user_id, 1000000) as distinct_count
FROM
[Orders]
WHERE
order_date >= DATE_ADD(UTC_USEC_TO_MONTH(CURRENT_DATE()),<Parameters.Offset>-12,"MONTH")
AND
order_date < DATE_ADD(UTC_USEC_TO_MONTH(CURRENT_DATE()),<Parameters.Offset>-1,"MONTH")
However, BigQuery always returns an error:
Error: DATE_ADD 2nd argument must have INT32 type.
When I try the same query in the BigQuery editor using simple arithmetic it fails with the same error.
SELECT
DATE_ADD(UTC_USEC_TO_MONTH(CURRENT_DATE()),5-3,"MONTH") as month_index,
FROM [Orders]
Any workaround for this? My only option so far is to make multiple offsets in Tableau, it seems.
Thanks for the help!
I acknowledge that this is a hole in functionality of DATE_ADD. It can be fixed, but it will take some time until fix is rolled into production.
Here is a possible workaround. It seems to work if the first argument to DATE_ADD is a string. Then you can truncate the result to a month boundary and convert it from a timestamp to a string.
SELECT
FORMAT_UTC_USEC(UTC_USEC_TO_MONTH(DATE_ADD(CURRENT_DATE(),5-3,"MONTH"))) as month_index;

How to get 6 months from a parametrized date

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())

finding time difference in DB2

How can I write a query in DB2 for following thing:
The difference between current timestamp and a timestamp field in dB should be >=4 hours AND <= 24 hours
Someone suggested this but it's not working.
select * from tableName where
date <= DATEADD([hour], -4, CURRENT_TIME) and
date date >= DATEADD([hour], -24, CURRENT_TIME)
But it's not working. It's giving following error.
SQL0104N An unexpected token "[hour]" was found following "ortdate <=
DATEADD(". Expected tokens may include: "<func_arg_list>". SQLSTATE=42601
select *
from table t
where t.tscolumn between current timestamp - 24 hours
and current timestamp - 4 hours
Use just Hour instead of [hour]
select * from tableName where
date <= DATEADD(Hour, -4, CURRENT_TIME) and
date date >= DATEADD(Hour, -24, CURRENT_TIME)
DB2 doesn't like square brackets around name - that is a MS SQL Server mannerism.
The only reference to DATEADD() in the DB2 9.7 Info Centre (oh, beg its pardon: Center - one day, American's will learn to spell correctly) is in 'All of the following expressions are in the package com.ibm.alphablox.bloxbuilder.lib.expression', which is puzzling. I suspect the search is erroneous - though going to the SQL Manual and finding the functions listed there, DATEADD is conspicuously absent, so maybe it isn't.
So, you are going to have to manual bash for the DB2 syntax. But, if anything is going to work, it is likely to involve:
DATEADD(HOUR, -4, CURRENT_TIME)
rather than any square brackets. However, a somewhat more extensive search, including the RedBook on DB2 and Oracle Compatibility, does not show DATEADD as a function that is supported by DB2. So, the DATEADD route is doomed to ... have problems.
Since DB2 (still) doesn't have a proper (SQL standard) INTERVAL type, you are into investigating 'durations'. See DevX for an explanation - but beware the number of cookies the site '.qnsr.com' wants to set. And read the manuals at the DB2 Info Centre.

SQL: How to display records for only 30 days

I tried to use
SELECT * from Results
WHERE DATEDIFF(d,Date,getdate())<30
But it seem having error with this.
For each record submitted will only display 30 days. May I know if my syntax is correct?
Greatly Appreciated,
Stan
Syntax looks OK, but you might need to 'quote' Date:
SELECT * from Results WHERE DATEDIFF(d, [Date], getdate()) < 30
Do you have a column called Date in Results?
BTW, that won't be able to use an index, whereas this will:
SELECT * from Results WHERE [Date] >= DATEADD(d, -30, getdate())
First off, you (and most of the replies in this thread) are mixing up SQL variants. You said nothing in your question about SQL Server, yet, you're getting recommendations on using SQL Server syntax (i.e., GetDate()).
The answer from JohnFx provides you correct Jet SQL syntax:
SELECT *
FROM results
WHERE ([Date] between DateAdd("d", -30, Date()) and Date())
But he is also correct that naming a field "Date" is really bad in Access/Jet. The WHERE clause might be improved with:
WHERE (results.Date between DateAdd("d", -30, Date()) and Date())
but I can't say for sure -- I would never name a field "Date" so would never encounter this kind of problem.
But there may be a simpler version, given that Jet stores its dates in a format where the integer part indicates the date and the decimal part the time. Because of that, when dealing with dates (as opposed to weeks or months or quarters), you can perform date math on them directly:
WHERE results.Date BETWEEN results.Date-30 AND Date()
This will give you exactly the same results as JohnFx's DateDiff() version, but won't need to call the DateAdd function for each row.
The key thing is using the proper syntax for a Jet database, and that means that the first argument for DateAdd() is a string value ("d") and that you can't use a SQL Server function (GetDate()), but must instead use Jet's function for the same purpose (Date()). But it's also a good idea to avoid using Jet/Access functions in your SQL when you don't have to, and that's why I believe that the "results.Date-30" version is going to be better than the DateAdd() version.
Aside: I really wish that those who post answers involving SQL would pay close attention to what database engine the questioner is using to execute the SQL. A lot of wrong answers are found in this thread precisely because those posters did not read the question carefully (it was pretty clear from the keywords what database engine was involved).
Try this:
SELECT *
FROM results
WHERE ([Date] between DateAdd("d", -30, Date()) and Date())
One other heads-up. Naming a field "Date" in Access is generally a bad idea. It is a reserved word and you will have to use brackets [] around it in all of your queries.
If you are using a Microsoft Access database, the function to get the current date is Date() rather than getdate() (that's for SQL Server).
Your query looks sound. What is the error that you're running in to?
You did not specify SQL Server as your db. In Access, the syntax for DateAdd is: DateAdd("d", 1, "31-Jan-95").