SQLite Forum

PRINTF - newlines, tabs etc?

PRINTF - newlines, tabs etc?

(1) By midijohnny on 2021-11-18 21:22:22 [source]

From the docs I understand that SQLite has its own implementation of 'printf', rather than matching the C implementation. - so it can provide some more specific formatting options.

But is there something fundamental about the way this is written that precludes the use of escaped characters for TAB "\t" , newline "\n" etc? (maybe platform independence issues with this?)

I found a workaround, so not a huge problem - but just more wordy and less standard:

SELECT PRINTF("x%cx",char(9)) -- works, just not the same in C (etc)
-- no rows: "\t" is returned literally rather than outputting a TAB.

(2) By Larry Brasfield (larrybr) on 2021-11-18 21:46:39 in reply to 1 [link] [source]

The printf() function does not interpret backslash-escape sequences in either C or SQLite. That is something that happens during translation, by a C or C++ compiler, from string literals in the source text to char sequences in the compiled output. And since SQLite does not implement the non-SQL convention of backslash escaping in its string literals, your examples would not work even if quoted correctly.

In SQL, the doublequote is used to delimit identifiers, not string literals. In SQL, a string literal is delimited by singlequote characters, except where they are paired within the quoted text.

(3) By midijohnny on 2021-11-22 14:08:25 in reply to 2 [link] [source]

Thank you for the clarification and explanation. So: I will use the following format going forward (single quotes, not double).

SELECT PRINTF('x%cx',char(9))

(4) By anonymous on 2021-11-22 16:16:13 in reply to 3 [link] [source]

String concatenation works too and may be more readable in some cases.

    SELECT 'x'||char(9)||'x'