Skip to Content
Technical Articles
Author's profile photo Johan Wigert

What is the difference between ` and ‘ in ABAP?

When defining a variable containing a text of some sort in ABAP, which of the following options do you typically use?

" Option 1
DATA(my_text) = `my text`.

 " Option 2
DATA(my_text) = 'my text'.

Are you aware of the differences between these two lines of code? Let us look into the details.

Character literals

What we are looking at are two different types of character literals. The ABAP Keyword Documentation provides us with the following explanation:

“Character literals can be either text field literals or text string literals. A text field literal is a character string enclosed in single quotation marks ('); a text string literal is a character string enclosed in single backquotes (`).”

Revisiting our example, we can define the variables like this:

" Option 1
DATA(my_text_string_literal) = `my text`.

" Option 2
DATA(my_text_field_literal) = 'my text'.

my_text_string_literal has the actual type STRING and the technical type CString of length 7. my_text_field_literal has a generated actual type (e.g. %_T00006S00000000O0000000298) and the technical type C of length 7.

The text field literal can have a length between 1 and 255 characters, whereas a text string literal can have a length between 0 and 255 characters.

That the text field literal has a minimum length of 1 means that '' (no space) has the same meaning as ' ' (a space character).

When to use which character literal?

The SAP Styleguide for clean ABAP contains the following recommendation:

“Refrain from using ', as it adds a superfluous type conversion and confuses the reader whether he’s dealing with a CHAR or STRING”

The following is an example of a superfluous type conversion between CHAR (from the text field literal) and a STRING:

DATA example_string TYPE string.
example_string = 'QWERTY'.

Prefer the following assignment:

DATA example_string TYPE string.
example_string = `QWERTY`.

If however the data element of the variable has been defined as a CHAR, the text field literal should be used to avoid a type conversion:

DATA e_mail_address TYPE ad_smtpadr. " CHAR 241
e_mail_address = 'mail@test.com'.

I hope that this short post helped clarify the difference between ' and `. Happy coding!

This blog post first appeared on the Developer Voyage blog at https://www.developervoyage.com/2019/09/02/what-is-the-difference-between-and-in-abap.html

Assigned Tags

      10 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sandra Rossi
      Sandra Rossi

      %_T00006S00000000O0000000298 is the name of a type dynamically generated, called “bound data type“.

      Risk of confusion: you are talking about the lengths of literals (maximum length of a literal in the ABAP editor), but people are probably more interested by the maximum lengths of the C and STRING types: character fields can have a length between 1 and 262.143 characters, whereas a character string can have a length between 0 and 2GB bytes (in Unicode systems, all systems with ABAP 7.50 and + are Unicode, one character equals 2 bytes).

      Note that a text field literal can have a length of 0, although it corresponds internally to a type C of minimum 1 character.

      Note that an important topic of character literals is the trap of trailing space characters. cf these two blog posts:

      You might also talk about string concatenations (&&).

       

      Author's profile photo Johan Wigert
      Johan Wigert
      Blog Post Author

      Thanks for a very insightful comment!

      Author's profile photo John Watson
      John Watson

      It is good to understand the difference between the operators; very nicely described from your side with good examples, overall it will clear out the doubts of “Noobs” like myself and lastly a good write up. 

      Author's profile photo Gopi DAMARAKUNTA
      Gopi DAMARAKUNTA

      Very well explained . Thanks for that

      Author's profile photo Joe Carter
      Joe Carter

      Thanks for giving the details about the operators, quite interesting to find out the difference.

       

       

      Regards,

      Joe 

      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      When your blog attracts the spam comments you know you made it! 🙂

      Thanks for sharing! That explains a lot. Don't remember the details but just recently I was trying a code example and could not understand why it didn't work as I expected. After super-careful examination I found it was using ` and not '. Had no idea it was even the thing and would have never found this in the documentation on my own.

      Author's profile photo Johan Wigert
      Johan Wigert
      Blog Post Author

      Great to hear! Thanks for your comment!

      Author's profile photo Simon Deutschl
      Simon Deutschl

      Thanks for this blog! I even didn't know neither that there's a difference between a single quotation and a singe backquote nor that it's possible to use the backquotes. I love such small blog posts filled with useful information.

      Author's profile photo Andreas Plewe
      Andreas Plewe

      Hi Johan,

      thank you very much for the explanation I recently searched in the ABAP documentation. I was very happy to find a link to the documentation in your blog. Unfortunately https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/index.htm?file=abennames_repos_obj_guidl.htm is wrong. The correct URL is https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/index.htm?file=abenuntyped_character_literals.htm. Clicking on a link in the navigation area does not change the URL, but executes a javascript command. In this case it's

      javascript:call_link('abenuntyped_character_literals.htm')

      Changing the file name to abenuntyped_character_literals.htm in your URL does the trick.

      Best regards,
      Andreas

       

      Author's profile photo Johan Wigert
      Johan Wigert
      Blog Post Author

      Hi Andreas,

       

      Thanks, I've corrected the link in the post now!

       

      Kind regards,

      Johan