[英]How to retrieve a substring after a delimiter from sql in Django?
How to retrieve a substring after a delimiter from sql in Django?如何在 Django 中的 sql 的分隔符之后检索 substring?
For example, I have "2021-22:32" saved in my sql database.例如,我的 sql 数据库中保存了“2021-22:32”。 I wish to run a query which returns "32", ie substring after the delimiter ':'.
我希望运行一个返回“32”的查询,即分隔符':'之后的substring。
How can I do that in Django?我该如何在 Django 中做到这一点?
Say my model is说我的 model 是
class model(models.Model):
str=models.CharField(max_length=25)
Suggest a query which returns all the entities, but with str values as explained above.建议一个返回所有实体的查询,但具有如上所述的 str 值。
In pure Python, you have the following options:在纯 Python 中,您有以下选项:
inp = "2021-22:32"
output = inp.split(':')[1] # 32
output = re.sub(r'^.*:', '', inp) # 32
On the SQL side, you could use similar options, depending on what your particular database vendor actually is.在 SQL 方面,您可以使用类似的选项,具体取决于您的特定数据库供应商实际上是什么。 For instance, on Postgres you have the
SPLIT_PART
function available:例如,在 Postgres 上,您可以使用
SPLIT_PART
function:
SELECT SPLIT_PART('2021-22:32', ':', 2); -- 32
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.