[SalesForce] SQL Query with IF and Concat statement possible in SFMC

I am wondering if this SQL- statement could some how work in Salesforce Marketing Cloud. I always got the error message:

An error occurred while checking the query syntax. Errors: Incorrect
syntax near the keyword 'IF'.

SELECT

IF LetterSalutationMCD('Sehr geehrte Frau')
       SELECT ('Sehr geehrte Frau'+Lastname) as Anrede,
IF LetterSalutation('Sehr geehrter Herr')<br><br>
      SELECT ('Sehr geehrter Herr'+Lastname) as Anrede,
ELSE <br><br>

SELECT ('Sehr geehrte Damen und Herren') as Anrede

Best Answer

This won't work as it isn't a correct SQL statement as you mix HTML and functions not present in Microsoft SQL 2005 (which is the core of Marketing Cloud SQL queries). Furthermore also user defined functions aren't available:

Unsupported elements:

  • Variables
  • Cursors
  • User Defined Functions
  • Transaction and Locking
  • GOTO
  • PRINT
  • Any sp_* stored procedure
  • EXEC
  • Temporary Tables and Common Table Expressions
  • TEXT and IMAGE Functions

(Source: Marketing Cloud documentation: SQL Reference)

To use IF-statements correctly, you need to omit the leading SELECT, like described in the following documentation article: IF...ELSE (Transact-SQL)

To start learning how to use SQL query activities I suggest reading the following documentation articles:

Related Topic