1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
16 HREF="sql-commands.html"><LINK
18 TITLE="CREATE AGGREGATE"
19 HREF="sql-createaggregate.html"><LINK
21 TITLE="CREATE CONSTRAINT TRIGGER"
22 HREF="sql-createconstraint.html"><LINK
25 HREF="stylesheet.css"><META
27 CONTENT="2003-12-22T03:48:47"></HEAD
33 SUMMARY="Header navigation table"
43 >PostgreSQL 7.4.1 Documentation</TH
51 HREF="sql-createaggregate.html"
60 HREF="sql-createaggregate.html"
73 HREF="sql-createconstraint.html"
81 HREF="sql-createconstraint.html"
102 >CREATE CAST -- define a new cast</DIV
107 CLASS="REFSYNOPSISDIV"
129 [ AS ASSIGNMENT | AS IMPLICIT ]
139 [ AS ASSIGNMENT | AS IMPLICIT ]</PRE
144 NAME="SQL-CREATECAST-DESCRIPTION"
152 > defines a new cast. A cast
153 specifies how to perform a conversion between
154 two data types. For example,
156 CLASS="PROGRAMLISTING"
157 >SELECT CAST(42 AS text);</PRE
159 converts the integer constant 42 to type <TT
163 invoking a previously specified function, in this case
167 >. (If no suitable cast has been defined, the
171 > Two types may be <I
173 >binary compatible</I
175 means that they can be converted into one another <SPAN
179 > without invoking any function. This requires that
180 corresponding values use the same internal representation. For
181 instance, the types <TT
191 > By default, a cast can be invoked only by an explicit cast request,
192 that is an explicit <TT
222 > If the cast is marked <TT
225 > then it can be invoked
226 implicitly when assigning a value to a column of the target data type.
227 For example, supposing that <TT
236 CLASS="PROGRAMLISTING"
237 >INSERT INTO foo (f1) VALUES (42);</PRE
239 will be allowed if the cast from type <TT
251 (We generally use the term <I
255 > to describe this kind of cast.)
258 > If the cast is marked <TT
261 > then it can be invoked
262 implicitly in any context, whether assignment or internally in an
263 expression. For example, since <TT
272 CLASS="PROGRAMLISTING"
273 >SELECT 'The time is ' || now();</PRE
275 will be allowed only if the cast from type <TT
286 will be necessary to write the cast explicitly, for example
288 CLASS="PROGRAMLISTING"
289 >SELECT 'The time is ' || CAST(now() AS text);</PRE
291 (We generally use the term <I
295 > to describe this kind of cast.)
298 > It is wise to be conservative about marking casts as implicit. An
299 overabundance of implicit casting paths can cause
303 > to choose surprising
304 interpretations of commands, or to be unable to resolve commands at
305 all because there are multiple possible interpretations. A good
306 rule of thumb is to make a cast implicitly invokable only for
307 information-preserving transformations between types in the same
308 general type category. For example, the cast from <TT
315 > can reasonably be implicit, but the cast from
323 assignment-only. Cross-type-category casts, such as <TT
330 >, are best made explicit-only.
333 > To be able to create a cast, you must own the source or the target
334 data type. To create a binary-compatible cast, you must be superuser.
335 (This restriction is made because an erroneous binary-compatible cast
336 conversion can easily crash the server.)
358 > The name of the source data type of the cast.
368 > The name of the target data type of the cast.
381 > The function used to perform the cast. The function name may
382 be schema-qualified. If it is not, the function will be looked
383 up in the path. The argument type must be identical to the
384 source type, the result data type must match the target type of
391 >WITHOUT FUNCTION</TT
395 > Indicates that the source type and the target type are binary
396 compatible, so no function is required to perform the cast.
406 > Indicates that the cast may be invoked implicitly in assignment
417 > Indicates that the cast may be invoked implicitly in any context.
426 NAME="SQL-CREATECAST-NOTES"
434 > to remove user-defined casts.
437 > Remember that if you want to be able to convert types both ways you
438 need to declare casts both ways explicitly.
444 > 7.3, every function that had
445 the same name as a data type, returned that data type, and took one
446 argument of a different type was automatically a cast function.
447 This convention has been abandoned in face of the introduction of
448 schemas and to be able to represent binary compatible casts in the
449 system catalogs. (The built-in cast functions still follow this naming
450 scheme, but they have to be shown as casts in the system catalog <TT
460 NAME="SQL-CREATECAST-EXAMPLES"
465 > To create a cast from type <TT
472 > using the function <TT
477 CLASS="PROGRAMLISTING"
478 >CREATE CAST (text AS int4) WITH FUNCTION int4(text);</PRE
480 (This cast is already predefined in the system.)
486 NAME="SQL-CREATECAST-COMPAT"
494 > command conforms to SQL99,
495 except that SQL99 does not make provisions for binary-compatible
509 NAME="SQL-CREATECAST-SEEALSO"
515 HREF="sql-createfunction.html"
521 HREF="sql-createtype.html"
527 HREF="sql-dropcast.html"
539 SUMMARY="Footer navigation table"
550 HREF="sql-createaggregate.html"
568 HREF="sql-createconstraint.html"
578 >CREATE AGGREGATE</TD
584 HREF="sql-commands.html"
592 >CREATE CONSTRAINT TRIGGER</TD