Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
hic
Former Employee
Former Employee

Escape sequences are a general technique to represent characters that are not possible to represent directly. In QlikView the need is mainly for quotation marks, but in the general case it could also be strange characters, tabs, and newlines. How this is handled in QlikView is explained in this post.

When writing a string in QlikView, it must usually be enclosed in single quotes. But what if you want to use a single quote inside the string? The following will not work:

 

     Set variable = 'This year's number';

 

The reason is that the apostrophe in the word year’s will be interpreted as the single quote that ends the string. So what should you do instead?

 

One solution that I have seen often in the community is to hard-code it using the Chr() function:

 

Let variable = 'This year' & Chr(39) & 's number' ;
Let variable = Replace( 'This year#s number', '#', Chr(39) ) ;

 

Both these work fine, but they are maybe not very elegant. Instead, I would suggest one of the following methods. First, you can often use a different delimiter:

 

Set variable = [This year's number];

 

As you can see, the Set statement can also use square brackets (or double quotes) as delimiters. Hence, if you just use a delimiter that is different from what you have in the string, it will work. The same is true if you need to load from a file with a name that contains single quotes or square brackets. Just make sure you quote it using double quotes (which is a character that shouldn't exist in file names):

 

LoadFrom "This year's numbers [3].xlsx"

 

But there is a second way this problem can be solved. An escape sequence:

 

Let variable = 'This year''s number';

 

The Let statement is different from the Set statement in that you must use single quotes as delimiter for literals. So you need to use an escape sequence instead: Just write the single quote twice, and the two characters will not be interpreted as a string delimiter, but instead as a single instance of the character itself. The same method can be used in other places also, e.g. in Set Analysis. The following expression is a correct one picking out the records from Robert's unit:

 

     Sum({1<Unit={'Robert''s unit'}>} Amount)

 

An escape sequence can be used for double quotes and square brackets, too. So if you have a field name that contains double quotes, for instance Name”5, you can load it either by using square brackets or by escaping the double quote:

 

[Name"5] as Field1,
"Name""5" as Field2,

 

If the field name contains square brackets, e.g. a field called Name[5], only the right bracket needs to be escaped. Such a field can be loaded in either of the two following ways:

 

"Name[5]" as Field1,
[Name[5]]] as Field2,

 

With this, I hope you got some ideas about how to deal with odd characters and strangely named fields.

 

HIC

 

Further reading related to this topic:

QlikView Quoteology

22 Comments
Peter_Cammaert
Partner - Champion III
Partner - Champion III

You're right Henric (as always). It was a bit difficult to detect, as I only had the terminating semicolon getting stored with the string.

Again misled by the QV documentation/help that says, The let statement, in opposition to the set statement, evaluates the expression on the right side of the '=' (indeed, from the LET help) I thought not evaluating the right hand side meant not deciding whether this is a string or not. Just look for the terminating semicolon. Sorry. QV is still very capable at dodging expectations

6,862 Views
Peter_Cammaert
Partner - Champion III
Partner - Champion III

This is a very creative example of QV script parser behavior with respect to quoting:

SET vVariable = ]/*[;

This syntax is perfectly valid. It will only store the initial closing bracket in vVariable. The opening bracket will disappear due to the opening comment, but the semicolon will function as expected (?). It will terminate the current statement. All subsequent (non-wacky) statements will execute as expected.

Not true: the comment will wipe out all remaining script text (or until it encounters its counterpart). The syntax checker however doesn't obey the same rules as the script engine, it seems.

0 Likes
6,862 Views
Not applicable

Thank you!!! I have got to remember this one - so helpful....

0 Likes
6,814 Views
IAMDV
Luminary Alumni
Luminary Alumni

Thank you HIC. Very useful to know. I'm glad that extra quote used as escape character is no stranger to the QV syntax parser.

0 Likes
6,814 Views
Not applicable

Thank you for sharing the article

0 Likes
6,814 Views
kkkumar82
Specialist III
Specialist III

Hi HIC,

replace(QuarterYear,''', 'FY') I had this situation so I replaced the second argument with chr(39) as

below replace(QuarterYear, chr(39) , 'FY'). My ask is Is there a way in the above to still use single quote as second arguments.

Hope I am clear

Thanks

Kiran Kumar

0 Likes
6,814 Views
hic
Former Employee
Former Employee

I would have expected the following to work:

   Replace(QuarterYear,'''', 'FY')

... but it doesn't. I'll investigate.

Bottom line is that right now I don't see any other way than to use Chr(39)

HIC

0 Likes
6,814 Views
Saravanan_Desingh

Hi HIC,

Is there a way to escape \n or \t in a text?

I could not find any solution, other than removing it.

Escape \n

0 Likes
6,814 Views
m_r_de_wit
Contributor III
Contributor III

Hi all,

Lot of useful information concernig quotation, but not my quotation problem.

I've a variable containing a text starting with a single quote (chr(39)).

I'm trying to use this variable (within $()) as literal in a Load statement, but as literals should come in single quotes it doesn't work.

Here is my code:

The QV parser inserts Escape sequence trying to solve the problem.

My variable vFile contains a single quote:

But the error comes with following debug information, the literal is starting with a triple single quote:

According to previous information, I would assume that the second and third quote would resolve as Escape sequence, but that doesn't work. My guess is doesn't work because it's at the beginning of the literal.

My simple solution is to replace the single quote by quotation mark, that works.

However my style is 'perfect and simple' solutions: so I'm still looking for a better wat to solve this.

Anyone a simple and perfect solution?

0 Likes
6,991 Views
fmcrashid
Contributor II
Contributor II

Hi Henric
I am having issue in generating script file for automating the tshared cleanup process. this my code to generate a batch file.

LOAD 'QVS.exe -x ' & Fname & ' -v -l C:\Temp' AS cls Resident FileList;
STORE ListOfSharedFile INTO .\_VerificationScript.bat (txt);

this works and file generated 

OUTPUT:

cls
QVS.exe -x C:\temp\EmptyShared - Copy.TShared -v -l C:\Temp
QVS.exe -x C:\temp\corrupt - Copy.TShared -v -l C:\Temp

but because file name may have spaces i have to wrap it in quotes, If I add char(34) then out file getting additional quotes

LOAD 'QVS.exe -x '&chr(34) & Fname & ' -v -l E:\Temp' AS cls Resident FileList;
STORE ListOfSharedFile INTO .\_VerificationScript.bat (txt);

OUTPUT:

cls
"QVS.exe -x ""C:\temp\EmptyShared - Copy.TShared -v -l C:\Temp"
"QVS.exe -x ""C:\temp\corrupt - Copy.TShared -v -l C:\Temp"

what i am trying to achieve is like this

cls
QVS.exe -x "C:\temp\EmptyShared - Copy.TShared" -v -l C:\Temp
QVS.exe -x "C:\temp\corrupt - Copy.TShared -v -l" C:\Temp

 

Any suggestions? help please

0 Likes
1,022 Views