2

I have a string field in a Firebird database. The data in some rows contains an ampersand. I'm trying to select the rows but the result is an empty dataset.

Using Delphi 12, FireDAC (TFDConnection + TFDQuery) and Firebird.

Example: Field1 in table Table1 contains one row with value "M&T1234". I try to select this row with the statement

select * from Table1 where Field1 = 'M&T1234'

The result is an empty dataset.

I know I can use a parameterised query, and this does work, but for various reasons I need to stick with the query shown.

Interestingly, if there is a space after the ampersand then it works as expected. It also works from the SQL editor in FB Maestro.

I've tried escaping the ampersand without success. What is the correct way to specify the value for a field containing an ampersand?

asked Mar 26, 2025 at 8:04
5
  • 2
    docwiki.embarcadero.com/RADStudio/Athens/en/… Commented Mar 26, 2025 at 12:13
  • @da-soft You process them even in delimited strings. What's the purpose? Commented Mar 26, 2025 at 12:33
  • 1
    This is not Firebird that does it, so it looks like it is FireDAC Commented Mar 26, 2025 at 17:50
  • @user13964273 da-soft only points to the FireDAC documentation; it isn't their product. Commented Mar 26, 2025 at 17:51
  • Thank you for your responses. As Remy suggested, turning off MacroExpand fixed the problem. Commented Mar 26, 2025 at 18:11

2 Answers 2

4

FireDAC preprocesses the raw SQL text before passing it to the DBMS for further processing. Sounds like the macro substitution is not aware of the SQL syntax, thus not affected by string quoting.

If you are not using FireDAC Macros, you can turn off the TFDQuery.ResourceOptions.MacroExpand property. Or disable all preprocessing by turning off the TFDQuery.ResourceOptions.PreprocessCmdText property.

answered Mar 27, 2025 at 7:25
Sign up to request clarification or add additional context in comments.

Comments

1

It looks like your SQL tool is scanning for substitution variables. You can usually escape the ampersand with a backslash or turn off scanning. In GoldSqall this can be done in the options or by using a "set scan off;" command in script. Your tool probably has something similar.

answered Mar 26, 2025 at 10:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.