Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Connect 2026! Turn data into bold moves, April 13 -15: Learn More!
cancel
Showing results for 
Search instead for 
Did you mean: 
amit_shetty78
Creator II
Creator II

Construct a string with single quotes in it

Hi All,

I am trying to create a sql statement dynamically from a variable:

LET Condition = 'WHERE TimeStart >=' & date(fieldValue('maxdate', 1),'YYYY-MM-DD hh:mm:ss.sss');
I am further using this variable in a SQL query:
LOAD a, b, c;
select a, b, c from ExecTbl
$(Condition)
;


This is giving an error because the generated SQL statement is:
select a, b, c from ExecTbl WHERE TimeStart >= 2009-08-09 01:02:53.403;

I would like it to be:
select a, b, c from ExecTbl WHERE TimeStart >= '2009-08-09 01:02:53.403';

Pls advice.

Thnks, Amit.

1 Solution

Accepted Solutions
hector
Specialist
Specialist

Hi, you can concatenate the function Chr() with 39 as parameter (Chr(39)), this function gives you the ' char

or in a complex case use the replace like this

Let strSQL = ' where company in (#C1#,#C2#)';

Let strSQLv2 = replace(strSQL,'#',Chr(39));

and the result is

strSQLv2 = where company in ('C1','C2')

Regards

View solution in original post

3 Replies
Not applicable

Declare another variable,

vSign = '

and then flank your date value with the variable??


LET Condition = 'WHERE TimeStart >=' vSign& date(fieldValue('maxdate', 1),'YYYY-MM-DD hh:mm:ss.sss')&vSign;


hector
Specialist
Specialist

Hi, you can concatenate the function Chr() with 39 as parameter (Chr(39)), this function gives you the ' char

or in a complex case use the replace like this

Let strSQL = ' where company in (#C1#,#C2#)';

Let strSQLv2 = replace(strSQL,'#',Chr(39));

and the result is

strSQLv2 = where company in ('C1','C2')

Regards

amit_shetty78
Creator II
Creator II
Author

Thanks Chai and Hector. Suggestions provided by both of you are very useful.

Cheers, Amit.