4

What is the standard syntax for adding carriage returns in line feeds to raw SQL queries?

vfclists
  • 19,193
  • 21
  • 73
  • 92
  • I'm not quite sure what you are asking. Are you asking how to include literal carriage returns and line feeds in strings within SQL? As in, be able to insert a string that contains a carriage return, or select for something that includes a linefeed? – Brian Campbell Nov 21 '12 at 07:27
  • @BrianCampbell Yes. I want to be able to add text into memo fields so when it is displayed in text controls, the lines will break where it is so in the SQL? – vfclists Nov 21 '12 at 07:29

1 Answers1

2

The standard way to insert a carriage return or linefeed into a string literal in a query is to simply include it verbatim within the string literal. There are no escape characters in SQL strings; the only character that can't be included in a string is a single quote (') character, which can be escaped by doubling it.

For example, to insert a row containing a newline:

INSERT INTO table VALUES ('this is a string
with a newline');

If you want to control whether a carriage return, linefeed, or bot will be inserted, you will need to make sure that one or both of them appear in the literal source.

Some implementations implement C-style escape sequences; in these implementations, you could write '\n', '\r', '\r\n' to add newlines, carriage returns, or CRLFs. However, this syntax is non-standard, so it won't be portable to all databases.

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340