简体   繁体   中英

Select date field by year?

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

This is not possible with abap sql. If you want to execute query then pass range date field for the particular year.

Following is the smaple code for retrieving data of year 2017.

DATA lr_aedat TYPE RANGE OF ekko-aedat. DATA ls_aedat LIKE LINE OF lr_aedat.

ls_aedat-low = '20170101'.
ls_aedat-high = '20171231'.
ls_aedat-sign = 'I'.
ls_aedat-option = 'BT'.
APPEND ls_aedat TO lr_aedat.

SELECT * FROM EKKO
  INTO TABLE @data(t_result)
  WHERE
*     WAERS = 'USD'
     AEDAT IN @lr_aedat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM