Return a :class:`.Extract` construct. This is typically available as :func:`.extract` as well as ``func.extract`` from the :data:`.func` namespace. :param field: The field to extract. .. warning:: This field is used as a literal SQL string. **DO NOT PASS UNTRUSTED IN
(field: str, expr: _ColumnExpressionArgument[Any])
| 1241 | |
| 1242 | |
| 1243 | def extract(field: str, expr: _ColumnExpressionArgument[Any]) -> Extract: |
| 1244 | """Return a :class:`.Extract` construct. |
| 1245 | |
| 1246 | This is typically available as :func:`.extract` |
| 1247 | as well as ``func.extract`` from the |
| 1248 | :data:`.func` namespace. |
| 1249 | |
| 1250 | :param field: The field to extract. |
| 1251 | |
| 1252 | .. warning:: This field is used as a literal SQL string. |
| 1253 | **DO NOT PASS UNTRUSTED INPUT TO THIS STRING**. |
| 1254 | |
| 1255 | :param expr: A column or Python scalar expression serving as the |
| 1256 | right side of the ``EXTRACT`` expression. |
| 1257 | |
| 1258 | E.g.:: |
| 1259 | |
| 1260 | from sqlalchemy import extract |
| 1261 | from sqlalchemy import table, column |
| 1262 | |
| 1263 | logged_table = table( |
| 1264 | "user", |
| 1265 | column("id"), |
| 1266 | column("date_created"), |
| 1267 | ) |
| 1268 | |
| 1269 | stmt = select(logged_table.c.id).where( |
| 1270 | extract("YEAR", logged_table.c.date_created) == 2021 |
| 1271 | ) |
| 1272 | |
| 1273 | In the above example, the statement is used to select ids from the |
| 1274 | database where the ``YEAR`` component matches a specific value. |
| 1275 | |
| 1276 | Similarly, one can also select an extracted component:: |
| 1277 | |
| 1278 | stmt = select(extract("YEAR", logged_table.c.date_created)).where( |
| 1279 | logged_table.c.id == 1 |
| 1280 | ) |
| 1281 | |
| 1282 | The implementation of ``EXTRACT`` may vary across database backends. |
| 1283 | Users are reminded to consult their database documentation. |
| 1284 | """ |
| 1285 | return Extract(field, expr) |
| 1286 | |
| 1287 | |
| 1288 | def false() -> False_: |