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 Like in Select Query

Former Member
0 Likes
89,438

Hi all

I am new to ABAP so i don't know how relevant the question is.

I have a variable say svar.

I want a select query using LIKE that helps me fetch data that starts with svar.

Something like 'ABC%'.

The only thing here is that it is a variable so i cannot use: 'svar%' or svar+'%' or anything like that.

Kindly suggest.

Thanks and Regards

Avinash

11 REPLIES 11
Read only

JozsefSzikszai
Active Contributor
27,521

hi,

your variable has to contain 'SVAR' and %, than it will work:

WHERE field LIKE variable

varibale's value is 'SVAR%', all records will be selected where field starts with 'SVAR'

hope this helps

ec

Read only

Former Member
0 Likes
27,521

hi check this..

PARAMETERS srch_str(20) TYPE c.

CONCATENATE '%' srch_str '%' INTO srch_str.

DATA text_tab TYPE TABLE OF doktl.

SELECT *

FROM doktl

INTO TABLE text_tab

WHERE doktext LIKE srch_str.

% is for single char

  • is for string of char

Read only

27,521

sql_cond - LIKE:

``The use of the wildcard characters "_" and "%" corresponds to the standard of SQL.``

SQL LIKE:

Wildcard character Description Example

% Any string of zero or more characters. WHERE title LIKE '%computer%' finds all book titles with the word 'computer' anywhere in the book title.

_ (underscore) Any single character. WHERE au_fname LIKE '_ean' finds all four-letter first names that end with ean (Dean, Sean, and so on).

Read only

Former Member
0 Likes
27,521

Hi Avinash,

Change the variable name. In the select query just search with 'svar%' or svar+'%' . After the select query you can pass it into the variable. Try it out.

Regards,

Swapna.

Read only

former_member195383
Active Contributor
0 Likes
27,521

u can use like in the select query.

But if u want the comparision to be done with a variable..then u can not use like in that case.

one thing u can do..If u know the postioning of the letters in the table field that are to be compared with the var1. then u can achieve this. Supose u want to compare 4 characters in the table field starting with the 3rd digit.

then in the where condition u can write something as...

where zfield+2(4) = WF_VAR1.

Read only

0 Likes
27,521

I tried the same thing but it gave an error that no such field exists.

What I mean is if I modify the field on which the search is based..it says no such field exist.

Read only

0 Likes
27,521

Can you post the code.

so that i can see what is the error.

Read only

Former Member
0 Likes
27,521

Check the sample ././

How to use the LIKE in select statement

REPORT  ZTEST_SIMPLE.

data: svar(4).
data: it_flight type sflight_tab1.
svar = 'A%'.


select * from sflight
into table it_flight
where carrid like svar.

break-point.

Read only

Former Member
0 Likes
27,521

Thanks everyone, a small error on my part also.

Yes If I append the variable by the wild card character it works...

I was doing a small mistake with that.

Read only

0 Likes
27,521

SELECT CASE

WHEN pac.PAC_NOME LIKE 'RN%' THEN ESP.ESP_COD = 'PED'

ELSE ESP.ESP_COD

END ESPECIALIDADEID

-- v.ESPECIALIDADE as especialidadedescricao,

-- 'O' AS fonte

FROM

PAC

INNER JOIN HSP ON HSP.HSP_PAC = PAC.PAC_REG

INNER JOIN v_esp_medico v ON hsp.hsp_mde = v.MEDICO

INNER JOIN esp ON esp.ESP_NOME = v.ESPECIALIDADE

INNER JOIN LOC ON LOC.LOC_COD = HSP.HSP_LOC

INNER JOIN STR ON STR.STR_COD = LOC.LOC_STR

INNER JOIN SMK ON hsp.hsp_proc1 = smk.smk_cod

WHERE HSP_dthra >= :data_ini

AND hsp_dthra <= :data_final


SQL Error [42000]: [-5015] (at 68): Missing keyword:END

BECAUSE?

Read only

moshenaveh
Community Manager
Community Manager
27,521
Hello,

While we're happy that you've come to SAP Community to get an answer to your question, you posted your question as an answer in an old thread.
I've converted your answer to a comment, but even so -- posting in older threads is not the best way to get guidance.
If you're looking for help, you should ask a new question: https://answers.sap.com/questions/ask.html.
Here are some tips to help you craft an effective question for our community: https://community.sap.com/resources/questions-and-answers, https://developers.sap.com/tutorials/community-qa.html, https://groups.community.sap.com/t5/welcome-corner-discussions/advice-from-sap-champions-questions-a....
I encourage you to follow this guidance, as I'd really like to see you get a solution to your problem.
I hope you find this advice useful!