Thursday, January 3, 2019

Django ORM queryset substring on a column


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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/8998da5d-3e3b-4fcd-a675-e5fc01ab01e3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment