SQL中字符串的部分排序(Postgresql)

huangapple go评论65阅读模式
英文:

SQL order by part of string (Postgresql)

问题

I need some help with SQL sort query creation. I have data like

ref_num
24534535_R
11789999_S
34543049_S
23476243_R
45737458_S

I need to sort that data based on the last string (R first, then S)

How to sort it using postgresql?

I tried using substring and still not work

需要根据最后的字符串排序数据(首先是R,然后是S)。在PostgreSQL中如何进行排序?我尝试使用substring但仍然无法正常工作。

英文:

I need some help with SQL sort query creation. I have data like

ref_num
24534535_R
11789999_S
34543049_S
23476243_R
45737458_S

I need to sort that data based on the last string (R first, then S)

How to sort it using postgresql?

I tried using substring and still not work

答案1

得分: 1

你应该能够使用 right(string text, n integer) → text

select * from some_table order by right(ref_num, 1);
英文:

You should be able to use right(string text, n integer) → text:

select * from some_table order by right(ref_num, 1);

huangapple
  • 本文由 发表于 2023年4月13日 14:54:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定