Database independent to_char function in SQLAlchemy

I use SQLAlchemy to create database independent queries.

I ran into one problem with the function to_char.

Consider a simple query:

select to_char(id,'999') from xyz 

It works on Postgres, but MySQL does not support it.

How can I make this query database independent with SQLAlchemy?

+3
source share
3 answers

use the CAST function to convert a simple type. it is an SQL standard, and SQLAlchemy supports it directly through the cast () function: http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/expressions.html?highlight=cast#sqlalchemy.sql.expression.cast .

, SQLA extract(), SQL EXTRACT - , "", "", "" .., .

+3

,

cast(numberfield as char)
+2

You are looking formatin MySQL:

select format(id, '999') from xyz

To answer the all-in-one question: there is no cross-database for this. Each DBMS is very different, and there is no real standardization past fairly simple queries. This is especially true when using string manipulations.

0
source

Source: https://habr.com/ru/post/1714298/


All Articles