I wanna get all elements of table EKKO by year , but I don't know how.
This is my query:
SELECT * FROM EKKO INTO TABLE @data(RESULT) WHERE BUKRS = @CO_Code AND WAERS = 'USD'.
I tried extract() and year(), like this :
SELECT * FROM EKKO INTO TABLE @data(RESULT) WHERE BUKRS = @CO_Code
AND WAERS = 'USD' AND YEAR (TO_DATE (AEDAT, 'YYYY-MM-DD') = 2017).
but it throws error
A Boolean expression is required in positions starting with YEAR.
What should I do next to complete?
Here are the contents of the table EKKO:

DATE functions are not supported in OpenSQL. I think they are either HANA(only) functions or implemented to AbapSQL (A later Hana-specific version of SQL) through later updates on S4/Hana systems. The one you tried to use with TO_DATE is definitely a HANA function.
SELECT * FROM EKKO
WHERE BUKRS = @CO_Code AND WAERS = 'USD'
AND substring( AEDAT, 1, 4 ) = 2017
INTO TABLE @data(RESULT).
You can also use AEDAT like '2017%' or AEDAT like '2017____' (_ wildcard for 1 character). The more specific your criteria the faster your query will be.
Edit 1: OpenSQL Date functions are implemented with 7.51 they support calculations, but not extraction. SAP Hana SQL - Datetime functions lists the function you tried to use, but it's a SAP Hana SQL reference page.
Edit 2:
To clarify, Abap SQL is just a later (starting ABAP 7.53 and higher) iteration of Open SQL. Rename to "Abap SQL" is directly related to transition to HANA database and cease of support of DB engines other than HANA DB. Backwards compatibility stays, but "some features" (likely meaning new features) will only be supported by HANA DB.
As a side note, latest versions of Abap (together with Hana DB) brings support to numeric date formats (as an option to use instead of current CHAR8 format). Extraction of years/months from numeric date is not as simple as substring, so I assume support for those kind of functions should appear in 'Abap SQL'.
SELECT * FROM ekko
INTO TABLE @data(result)
WHERE [...]
AND aedat LIKE '2017%'.
The format you see in your screenshot is not what’s really in the database. SAP GUI formats values based on their data elements when displaying them.
Date values mostly have the data type DATS which encodes dates as CHAR(8) of the form YYYYMMDD. You can verify this by selecting a single AEDAT and using WRITE without any formatting options to output it on the screen in its raw format.
Thus, the simplest way to query by the year portion is with the condition LIKE '2017%' that matches every string that starts with "2017".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With