ABAP text field literal vs text string literal
Have you ever had a variable containing text? I am sure you did and regularly do. What is your preferred way to do declare it?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Option 1. | |
DATA: | |
lv_text TYPE string VALUE 'Some Text'. | |
"Option 2." Notice the ` | |
DATA: | |
lv_text2 TYPE string VALUE `Some other text`. |
First of all the declaration is a literal being passed to a variable, and some
of you may ask:
what is a literal?
Literals are defined in the source code of a program by specifying a string representing a value.
Possible literals are numeric literals and character
literals. The character literal comprises text
field literals and text string literals.
So there are two kinds of text literal, let's see them in detail.
Text Field Literals:
- Defined using single inverted commas (').- Data type is c with the length of the included characters, including trailing blanks.
- There is no empty text field literal:
This means, The text field literal '' is identical to the text field literal ' ' of length 1.
- The length of the text field literal( not of type c) is from 1 to 255.
Text String Literals:
- Defined using single backquotes (`).
- Data type is a string.
- The empty text string literal `` represents a string of length 0.
- The length of the text string literal( not of type string) is from 0 to 255.
- Trailing blanks are not ignored.
- Data type is a string.
- The empty text string literal `` represents a string of length 0.
- The length of the text string literal( not of type string) is from 0 to 255.
- Trailing blanks are not ignored.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REPORT sy-repid. | |
START-OF-SELECTION. | |
IF ' ' = ` `. | |
WRITE: 'Both literals are Same.'. | |
ELSE. | |
WRITE: 'Both literals are Not sameand have some difference.'. | |
ENDIF. |
The unexpected output is the manifestation of ABAP comparison rules of string to char or vice versa.
Armed with the new knowledge, the old example can be rewritten like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Option 1. text field. | |
DATA: | |
lv_text TYPE char10 VALUE 'Some Text'. | |
"Option 2." Text string. | |
DATA: | |
lv_text2 TYPE string VALUE `Some other text`. |
In Conclusion:
Appropriate text literal should be used when defining them to avoid superfluous type conversions.
The type to be used is determined by what type of variables they are going to be assigned.
Hope you liked the post and it helps you in your day to day work. Here is a good use example to
sign off.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DATA: | |
lv_log_String TYPE STRING, | |
lv_mail_id TYPE ad_smtpadr. | |
" Assigning values.. | |
lv_log_string = `Assigning Email ID`. | |
lv_mail_id = 'someuser@domain.com'. |