Skip to content

Commit df88137

Browse files
committed
Doc: improve description of allowed spellings for Boolean input.
datatype.sgml failed to explain that boolin() accepts any unique prefix of the basic input strings. Indeed it was actively misleading because it called out a few minimal prefixes without mentioning that there were more valid inputs. I also felt that it wasn't doing anybody any favors by conflating SQL key words, valid Boolean input, and string literals containing valid Boolean input. Rewrite in hopes of reducing the confusion. Per bug #15836 from Yuming Wang, as diagnosed by David Johnston. Back-patch to supported branches. Discussion: https://postgr.es/m/15836-656fab055735f511@postgresql.org
1 parent 8ace51a commit df88137

File tree

1 file changed

+43
-23
lines changed

1 file changed

+43
-23
lines changed

doc/src/sgml/datatype.sgml

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,37 +2885,36 @@ SELECT EXTRACT(days from '80 hours'::interval);
28852885
</table>
28862886

28872887
<para>
2888-
Valid literal values for the <quote>true</quote> state are:
2888+
Boolean constants can be represented in SQL queries by the SQL
2889+
key words <literal>TRUE</literal>, <literal>FALSE</literal>,
2890+
and <literal>NULL</literal>.
2891+
</para>
2892+
2893+
<para>
2894+
The datatype input function for type <type>boolean</type> accepts these
2895+
string representations for the <quote>true</quote> state:
28892896
<simplelist>
2890-
<member><literal>TRUE</literal></member>
2891-
<member><literal>'t'</literal></member>
2892-
<member><literal>'true'</literal></member>
2893-
<member><literal>'y'</literal></member>
2894-
<member><literal>'yes'</literal></member>
2895-
<member><literal>'on'</literal></member>
2896-
<member><literal>'1'</literal></member>
2897+
<member><literal>true</literal></member>
2898+
<member><literal>yes</literal></member>
2899+
<member><literal>on</literal></member>
2900+
<member><literal>1</literal></member>
28972901
</simplelist>
2898-
For the <quote>false</quote> state, the following values can be
2899-
used:
2902+
and these representations for the <quote>false</quote> state:
29002903
<simplelist>
2901-
<member><literal>FALSE</literal></member>
2902-
<member><literal>'f'</literal></member>
2903-
<member><literal>'false'</literal></member>
2904-
<member><literal>'n'</literal></member>
2905-
<member><literal>'no'</literal></member>
2906-
<member><literal>'off'</literal></member>
2907-
<member><literal>'0'</literal></member>
2904+
<member><literal>false</literal></member>
2905+
<member><literal>no</literal></member>
2906+
<member><literal>off</literal></member>
2907+
<member><literal>0</literal></member>
29082908
</simplelist>
2909+
Unique prefixes of these strings are also accepted, for
2910+
example <literal>t</literal> or <literal>n</literal>.
29092911
Leading or trailing whitespace is ignored, and case does not matter.
2910-
The key words
2911-
<literal>TRUE</literal> and <literal>FALSE</literal> are the preferred
2912-
(<acronym>SQL</acronym>-compliant) usage.
29132912
</para>
29142913

29152914
<para>
2916-
<xref linkend="datatype-boolean-example"> shows that
2917-
<type>boolean</type> values are output using the letters
2918-
<literal>t</literal> and <literal>f</literal>.
2915+
The datatype output function for type <type>boolean</type> always emits
2916+
either <literal>t</literal> or <literal>f</literal>, as shown in
2917+
<xref linkend="datatype-boolean-example">.
29192918
</para>
29202919

29212920
<example id="datatype-boolean-example">
@@ -2937,6 +2936,27 @@ SELECT * FROM test1 WHERE a;
29372936
t | sic est
29382937
</programlisting>
29392938
</example>
2939+
2940+
<para>
2941+
The key words <literal>TRUE</literal> and <literal>FALSE</literal> are
2942+
the preferred (<acronym>SQL</acronym>-compliant) method for writing
2943+
Boolean constants in SQL queries. But you can also use the string
2944+
representations by following the generic string-literal constant syntax
2945+
described in <xref linkend="sql-syntax-constants-generic">, for
2946+
example <literal>'yes'::boolean</literal>.
2947+
</para>
2948+
2949+
<para>
2950+
Note that the parser automatically understands
2951+
that <literal>TRUE</literal> and <literal>FALSE</literal> are of
2952+
type <type>boolean</type>, but this is not so
2953+
for <literal>NULL</literal> because that can have any type.
2954+
So in some contexts you might have to cast <literal>NULL</literal>
2955+
to <type>boolean</type> explicitly, for
2956+
example <literal>NULL::boolean</literal>. Conversely, the cast can be
2957+
omitted from a string-literal Boolean value in contexts where the parser
2958+
can deduce that the literal must be of type <type>boolean</type>.
2959+
</para>
29402960
</sect1>
29412961

29422962
<sect1 id="datatype-enum">

0 commit comments

Comments
 (0)