Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Date difference in SQL Where statement

Former Member
0 Likes
826

I would like to do select in my database table like this:

select * from abc_table

where dtret - dt_current = 6.

where:

dtret is a date field in table,

dt_current has sy-datum.

I can do this? dtret - dt_current works well in all the cases? Or I have to use some function?

Thanks

1 ACCEPTED SOLUTION
Read only

ferry_lianto
Active Contributor
0 Likes
754

Hi,

Please try this.


DATA: WA_DATE LIKE SY-DATUM.

WA_DATE = SY-DATUM - 6.

SELECT * 
INTO TABLE ITAB
FROM ABC_TABLE
WHERE DTRET = WA_DATE.

Regards,

Ferry Lianto

7 REPLIES 7
Read only

Former Member
0 Likes
754

Sorry, I forgot to mention that I need to receive the delta in days.

Read only

ferry_lianto
Active Contributor
0 Likes
755

Hi,

Please try this.


DATA: WA_DATE LIKE SY-DATUM.

WA_DATE = SY-DATUM - 6.

SELECT * 
INTO TABLE ITAB
FROM ABC_TABLE
WHERE DTRET = WA_DATE.

Regards,

Ferry Lianto

Read only

Former Member
0 Likes
754

I don't think it is possible.

You can fetch the data first in an internal table.

LOOP through internal table and find out the difference between dtret and dt_current and if it is not equal to 6, delete this record from internal table.

Read only

Former Member
0 Likes
754

Hi

You can do this way.

Have a variable of type <b>sy-datum</b> and assign value as <b>sy-datum - 6</b>.

Then use this variable in WHERE condition as

<b>WHERE dtret = <your variable>.</b>

Regards,

Raj

Read only

former_member196280
Active Contributor
0 Likes
754

Hi Cristiana,

Apply the below code I guess it will solve your problem.

Select * into table itab from abc_table

where dtret NE sy-datum.

LOOP at itab.

IF sy-subrc EQ 0.

dt_current = itab-dtret - dt_current.

IF dt_current EQ 6.

MOVE itab TO itab1.

APPEND itab1.

ENDIF.

ENDIF.

ENDLOOP.

Reward points to all useful answers.

Regards,

SaiRam

Read only

0 Likes
754

My problem is that my abc_table has a looooooot of records....

So I would like to read to my internal table only those ones that match....

Read only

0 Likes
754

I think Ferry's example is exactly what you are looking for. Have you tried it?

Rob