Skip to Content

Select statement

In open sql select query i want to extract year from database column and compare it with entered value.

for eg.

i want to get employees joined in year 2005

for that i have to extract year from the database column and compare it with 2005

How is it possible in select query?

Please Reply soon i have to complete this today.

Thanks in advance,

Regards,

Bharat.

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

6 Answers

  • Best Answer
    author's profile photo
    Former Member
    Oct 07, 2005 at 07:42 AM

    Hi,

    you have not told what is the column type you used in the database table.

    lets consider the name of the column is join_date and type is of sy-datum, and your table name is zemp.

    then do the following.

    data: LT_zemp like zemp,

    ls_zemp like line of lt_zemp.

    data: limit_date like sy-datum values '20050101'. "1st jan 2005

    select * from zemp into ls_zemp.

    if ls_zemp-join_date >= limit_date.

    append ls_zemp to lt_zemp.

    endif.

    clear ls_zemp.

    endselect.

    please reward point if it answers your need.

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Oct 07, 2005 at 07:40 AM

    Your question is not so clear...

    please give more details about the column etc.

    I think yopu can use

    PARAMETERS PA_YEAR(4).

    SELECT * FROM DB_TAB INTO TABLE WHERE YEAR = PA_YEAR.

    If not satified give more details ...

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Oct 07, 2005 at 07:57 AM

    Hi Bharat,

    just one correction.

    in the internal table declartion part..

    instead of those like zemp statements..do the following

    types: begin of ty_zemp.

    include structure <your db table name>.

    types: end of ty_zemp.

    data: lt_zemp type ty_zemp,

    ls_zemp like line of lt_zemp.

    now declare the date "limit_date" as i wrote in previous post, and then do the select query as i already gave.

    let us know in case of problem..and also let us know the type of date column in your table, if it is not of type sy-datum.

    Does this solve your query??

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi,

      I suggest you usr the LIKE addition in the where clause.

      for example.

      imagine something like this is stored in the database.

      31 december 2004 = 20041231

      parameters : pa_year type year.

      start-of-selection.

      data : l_like(10) value '______'.

      concatenate year l_like into l_like.

      select * from db_tab where date like l_like.

      endselect.

      this will retrieve everything for yea entered by the end user

      PS :

      -NOTE THAT THE LIKE ADDITION is not the best optimized select statement! It is runtime expensive.

      -note that select...endselect is neither the best optimized select

      Message was edited by: STEPHAN KAMINSKI

  • author's profile photo
    Former Member
    Oct 07, 2005 at 08:09 AM

    Hi,

    Please follow this.

    PARAMETERS : P_YEAR TYPE <b>VBKD-GJAHR</b>.

    select x1 x2 from <b>tab_name</b> appending corresponding values of table t_itab where year =

    <b>P_YEAR</b>.

    t_itab is the internal table containing the details you need from the database table.

    Replace x1 , x2, year with the correct field values.

    Please reward points if this explanation is useful.

    Regards,

    Siva

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Oct 07, 2005 at 08:10 AM

    Try this.

    DATA WA(5).

    DATA WA_YEAR(4) VALUE '2005'.

    CONCATENATE WA_YEAR '%' INTO WA .

    SELECT * FROM <DBTAB>

    WHERE <DATE> LIKE WA.

    Cheers.

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Oct 07, 2005 at 08:13 AM

    Hi Bharat,

    thanks for your second mail..it cleared the doubt..here is the solution.please reward points if its ok.

    I just tested this , it works.your lt_tab tabel will contain all the ename whose begda(4) > 2005

    Data: begin of lt_tab occurs 0,

    ename like pa0001-ename,

    begda like pa0001-begda,

    end of lt_tab.

    select ename begda from pa0001 into lt_tab.

    if lt_tab-begda(4) eq '2005'.

    append lt_tab.

    endif.

    endselect.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Bharat Mistry

      Hi,

      here is the solution, for your updated query.

      DATA: START_DAT LIKE SY-DATUM VALUE '20050101',

      END_DAT LIKE SY-DATUM VALUE '20051231'.

      select single SUM( anzhl ) sum( kverb )

      into (SLQUOTA,slded)

      from pa2006

      where pernr = wa_pa0001-perno

      and ktart = 60

      and ( begda GE START_DAT OR

      begda LE END_DAT )

      group by ktart