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

Using wild chatacters in the query

Former Member
0 Likes
1,877

Hi experts,

I have a query like this:

IF NOT i_name1 IS INITIAL.

SELECT kunnr

FROM kna1

INTO TABLE it_kna1

WHERE name1 EQ i_name1.

ENDIF.

Lets says KNA1 has some customer names like CABD, ZABY, PABQ, LABN.

Suppose a customer enters the customer name as AB, how can I use the wild characters in the query to ensure

that all these 4 names are fetched?

Thanks,

Ajay.

1 ACCEPTED SOLUTION
Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,847

Use a [SELECT-OPTIONS|http://help.sap.com/abapdocu_70/en/ABAPSELECT-OPTIONS.htm] and not a PARAMETER for input and a [SELECT WHERE NAME IN SO-NAME |http://help.sap.com/abapdocu_70/en/ABENWHERE_LOGEXP_SELTAB.htm]statement to read.

SELECT-OPTIONS so-name1 FOR kna1-name1 NO-EXTENSION NO INTERVALS.
SELECT * FROM kna1 WHERE name1 IN so-name1.

Regards,

Raymond

17 REPLIES 17
Read only

Former Member
0 Likes
1,847

Hi,

You can use like this :

IF NOT i_name1 IS INITIAL.

SELECT kunnr

FROM kna1

INTO TABLE it_kna1

WHERE name1 like '%AB%'.

ENDIF.

Regards,

Sunny

Read only

0 Likes
1,847

Hi Sunny,

Thanks for the valuable response. Is it possible to do some dynamic coding for this % thing. I mean if the user does not know the customer name, he can enter any combination.

Thanks,

Ajay.

Read only

0 Likes
1,847

Hi Ajay,

Try like below,


PARAMETERS: pattern TYPE string.  " Enter dynamic combination of customer name
CONCATENATE '%' pattern '%' INTO pattern.

IF NOT i_name1 IS INITIAL.
SELECT kunnr
FROM kna1
INTO TABLE it_kna1
WHERE name1 like pattern.
ENDIF.

Thanks,

Read only

0 Likes
1,847

Hi,

If m not wrong, you are asking like if the pattern is changing then how it can be done?

IF that is the case, you should take that pattern into a variable concatenate % before and after the value and use this variable in your select query.

var = 'cd'.

concatenate '%' var '%' into var.

select abc

into xyz

from sss

where value like var.

Regards,

Sunny

Read only

0 Likes
1,847

hi ajay ,

who told it is not possible pls do this way

Don't worry You can use " Ranges : i_name1 like kn1-name1 . PASTE BELOW CODE IN YOUR FUNCTION MODULE

IT WILL RUN LIKE A BULLET .

Ranges : i_name1 like knA1-name1 .

MOVE 'I' TO i_NAME1-sign.

MOVE 'CP' TO I_NAME1 option.

MOVE I_NAME1 TO I_NAME1-low.

APPEND I_NAME1.

IF NOT i_name1 IS INITIAL.

SELECT kunnr

FROM kna1

INTO TABLE it_kna1

WHERE name1 EQ i_name1.

ENDIF.

rEGARDS

dEEPAK .

Read only

0 Likes
1,847

hi ajay ,

who told it is not possible pls do this way

Don't worry You can use " Ranges : i_name1 like kn1-name1 . PASTE BELOW CODE IN YOUR FUNCTION MODULE

IT WILL RUN LIKE A BULLET .

Ranges : i_name1 like knA1-name1 .

MOVE 'I' TO i_NAME1-sign.

MOVE 'CP' TO I_NAME1 option.

MOVE I_NAME1 TO I_NAME1-low.

APPEND I_NAME1.

IF NOT i_name1 IS INITIAL.

SELECT kunnr

FROM kna1

INTO TABLE it_kna1

WHERE name1 EQ i_name1.

ENDIF.

rEGARDS

dEEPAK .

Read only

Former Member
0 Likes
1,847

Hi,

Use


SELECT kunnr
FROM kna1
INTO TABLE it_kna1
WHERE name1 LIKE '%AB%'.

Read only

Former Member
0 Likes
1,847

Please SEARCH in SCN before posting basic questions

Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,848

Use a [SELECT-OPTIONS|http://help.sap.com/abapdocu_70/en/ABAPSELECT-OPTIONS.htm] and not a PARAMETER for input and a [SELECT WHERE NAME IN SO-NAME |http://help.sap.com/abapdocu_70/en/ABENWHERE_LOGEXP_SELTAB.htm]statement to read.

SELECT-OPTIONS so-name1 FOR kna1-name1 NO-EXTENSION NO INTERVALS.
SELECT * FROM kna1 WHERE name1 IN so-name1.

Regards,

Raymond

Read only

0 Likes
1,847

Hi Raymond,

I am doing all this coding inside a FM. I think Select Options or Parameters cant be used inside a FM.

Any other workaround?

Thanks,

Ajay.

Read only

0 Likes
1,847

Where did the user input their "keywords", you can use a [SELECTION-SCREEN - AS SUBSCREEN |http://help.sap.com/abapdocu_70/en/ABAPSELECTION-SCREEN_SUBSCREEN.htm]

Regards,

Raymond

Read only

0 Likes
1,847

Hi Ajay,

try this:


REPLACE ALL OCCURRENCES OF '*' in i_name1 WITH '%'.
IF NOT i_name1 IS INITIAL.
SELECT kunnr
FROM kna1
INTO TABLE it_kna1
WHERE name1 LIKE i_name1.
ENDIF.

Read only

0 Likes
1,847

Hi Ajay,

Since u are coding inside FM, do like below.

Create an importing parameter(mandatory) for the FM which will get customer name comibation into the function module.

(Ex: Pattern type String) Cutomer name combinations are like AB, BA, CD..,

Now inside FM add below statement,

CONCATENATE '%' pattern '%' INTO pattern.

IF NOT i_name1 IS INITIAL.

SELECT kunnr

FROM kna1

INTO TABLE it_kna1

WHERE name1 like pattern.

ENDIF.

(Note: While calling above FM from any program, parameter Pattern(customer name combination) has to be passed)

Ex:

Parameters: pattern type string.

CALL FUNCTION 'ZTESTSERCH'

IMPORTING

pattern = pattern.

Thanks,

Read only

0 Likes
1,847

Hi ajay,

also, the user will not be able to enter anything within a function module.

You can pass the select-options table body (SO_NAME1[] as import parameter of TYPE TABLE to the function module.

Regards,

Clemens

Read only

deepak_dhamat
Active Contributor
0 Likes
1,847

hi ajay ,

who told it is not possible pls do this way

Don't worry You can use " Ranges : i_name1 like kn1-name1 . PASTE BELOW CODE IN YOUR FUNCTION MODULE

IT WILL RUN LIKE A BULLET .

Ranges : i_name1 like knA1-name1 .

MOVE 'I' TO i_NAME1-sign.

MOVE 'CP' TO I_NAME1 option.

MOVE I_NAME1 TO I_NAME1-low.

APPEND I_NAME1.

IF NOT i_name1 IS INITIAL.

SELECT kunnr

FROM kna1

INTO TABLE it_kna1

WHERE name1 EQ i_name1.

ENDIF.

rEGARDS

dEEPAK .

Read only

deepak_dhamat
Active Contributor
0 Likes
1,847

hi ajay ,

who told it is not possible pls do this way

Don't worry You can use " Ranges : i_name1 like kn1-name1 . PASTE BELOW CODE IN YOUR FUNCTION MODULE

IT WILL RUN LIKE A BULLET .

Ranges : i_name1 like knA1-name1 .

MOVE 'I' TO i_NAME1-sign.

MOVE 'CP' TO I_NAME1 option.

MOVE I_NAME1 TO I_NAME1-low.

APPEND I_NAME1.

IF NOT i_name1 IS INITIAL.

SELECT kunnr

FROM kna1

INTO TABLE it_kna1

WHERE name1 EQ i_name1.

ENDIF.

rEGARDS

dEEPAK .

Read only

deepak_dhamat
Active Contributor
0 Likes
1,847

hi ajay ,

who told it is not possible pls do this way

Don't worry You can use " Ranges : i_name1 like kn1-name1 . PASTE BELOW CODE IN YOUR FUNCTION MODULE

IT WILL RUN LIKE A BULLET .

Ranges : i_name1 like knA1-name1 .

MOVE 'I' TO i_NAME1-sign.

MOVE 'CP' TO I_NAME1 option.

MOVE I_NAME1 TO I_NAME1-low.

APPEND I_NAME1.

IF NOT i_name1 IS INITIAL.

SELECT kunnr

FROM kna1

INTO TABLE it_kna1

WHERE name1 EQ i_name1.

ENDIF.

rEGARDS

dEEPAK .