Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Vous êtes un professionnel et vous avez besoin d'une formation ? Sensibilisation à
l'Intelligence Artificielle
Voir le programme détaillé
Module « sqlalchemy »

Fonction try_cast - module sqlalchemy

Signature de la fonction try_cast

def try_cast(expression: '_ColumnExpressionOrLiteralArgument[Any]', type_: '_TypeEngineArgument[_T]') -> 'TryCast[_T]' 

Description

help(sqlalchemy.try_cast)

Produce a ``TRY_CAST`` expression for backends which support it;
this is a ``CAST`` which returns NULL for un-castable conversions.

In SQLAlchemy, this construct is supported **only** by the SQL Server
dialect, and will raise a :class:`.CompileError` if used on other
included backends.  However, third party backends may also support
this construct.

.. tip:: As :func:`_sql.try_cast` originates from the SQL Server dialect,
   it's importable both from ``sqlalchemy.`` as well as from
   ``sqlalchemy.dialects.mssql``.

:func:`_sql.try_cast` returns an instance of :class:`.TryCast` and
generally behaves similarly to the :class:`.Cast` construct;
at the SQL level, the difference between ``CAST`` and ``TRY_CAST``
is that ``TRY_CAST`` returns NULL for an un-castable expression,
such as attempting to cast a string ``"hi"`` to an integer value.

E.g.::

    from sqlalchemy import select, try_cast, Numeric

    stmt = select(try_cast(product_table.c.unit_price, Numeric(10, 4)))

The above would render on Microsoft SQL Server as:

.. sourcecode:: sql

    SELECT TRY_CAST (product_table.unit_price AS NUMERIC(10, 4))
    FROM product_table

.. versionadded:: 2.0.14  :func:`.try_cast` has been
   generalized from the SQL Server dialect into a general use
   construct that may be supported by additional dialects.



Vous êtes un professionnel et vous avez besoin d'une formation ? Programmation Python
Les fondamentaux
Voir le programme détaillé