You can use a Func expression for this purpose[0]
Employee.objects.annotate(
emp_number_suffix=Func('emp_number', Value('-'), -1,
function='substring_index'),
).filter(
emp_number_suffix='111',
).values('emp_number')
Cheers,
Simon
[0]
https://docs.djangoproject.com/en/2.1/ref/models/expressions/#func-expressions
Le jeudi 3 janvier 2019 08:17:43 UTC-5, BIJAL MANIAR a écrit :
>
>
> Hello,
>
> Consider below column in mysql table Employee. I need to write django orm
> query for below mysql query.
> emp_number
> 4-DEF-A111
> 3-MNO-333
> 2-DEF-222
> 1-ABC-111
>
>
> Mysql query which splits by '-' and matches against last index.
> SELECT * from Employee WHERE substring_index(emp_number, '-', -1) = '111';
>
> I cannot write endswith on a column like below:
> Employee.objects.filter(emp_number__endswith='111').values('emp_number')
> This will return below 2 records:
> 4-DEF-A111
> 1-ABC-111
>
> Any help would be appreciated. Thanks.
>
> -Bijal
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/8b1aa18c-ab06-4ac1-912d-9cca0359e182%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.