| 343 | |
| 344 | |
| 345 | class Substr(Func): |
| 346 | function = "SUBSTRING" |
| 347 | output_field = CharField() |
| 348 | |
| 349 | def __init__(self, expression, pos, length=None, **extra): |
| 350 | """ |
| 351 | expression: the name of a field, or an expression returning a string |
| 352 | pos: an integer > 0, or an expression returning an integer |
| 353 | length: an optional number of characters to return |
| 354 | """ |
| 355 | if not hasattr(pos, "resolve_expression"): |
| 356 | if pos < 1: |
| 357 | raise ValueError("'pos' must be greater than 0") |
| 358 | expressions = [expression, pos] |
| 359 | if length is not None: |
| 360 | expressions.append(length) |
| 361 | super().__init__(*expressions, **extra) |
| 362 | |
| 363 | def as_sqlite(self, compiler, connection, **extra_context): |
| 364 | return super().as_sql(compiler, connection, function="SUBSTR", **extra_context) |
| 365 | |
| 366 | def as_oracle(self, compiler, connection, **extra_context): |
| 367 | return super().as_sql(compiler, connection, function="SUBSTR", **extra_context) |
| 368 | |
| 369 | |
| 370 | class Trim(Transform): |