function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jonathan Wolff 7Jonathan Wolff 7 

Create String variable with SQL Where Clause

Hello, I would like to create a string variable that contains a Where-clause.  I tried the following:
String AWhereClause=  'AND Typ__c = 'Infobroschüre'';
Unfortunatly that way does not work. Can you give me an solution for that?
Best Answer chosen by Jonathan Wolff 7
sarika shirkesarika shirke
In SQL, string values should be enclosed in single quotes, as you have done with the string literal 'Infobroschüre'. However, since you also have single quotes within the string, you need to escape them properly. Here's an example of how you can create a string variable with a SQL WHERE clause:
javascriptCopy code
String AWhereClause = "AND Typ__c = 'Infobroschüre'";
Note that we have used double quotes to enclose the string variable, which allows us to include single quotes without needing to escape them. You can use this variable in your SQL query by concatenating it with your SELECT statement or WHERE clause, like this:
javascriptCopy code
String sqlQuery = "SELECT * FROM MyTable WHERE Column1 = 'Value1' " + AWhereClause;
This will result in a SQL query that looks like this:
sqlCopy code
SELECT * FROM MyTable WHERE Column1 = 'Value1' AND Typ__c = 'Infobroschüre'
I hope this helps!