按字段的前两个字符对SQL排序 [英] Sorting SQL by first two characters of fields

查看:152
本文介绍了按字段的前两个字符对SQL排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按销售人员姓名首字母对一些数据进行排序,并且销售代表字段的长度为3个字符,分别为名字,姓氏和帐户类型.因此,鲍勃·史密斯(Bob Smith)将是BS *,我只需要按前两个字符进行排序.

I'm trying to sort some data by sales person initials, and the sales rep field is 3 chars long, and is Firstname, Lastname and Account type. So, Bob Smith would be BS* and I just need to sort by the first two characters.

在字段的前两个字符等于BS的情况下,如何提取特定代表的所有数据?

How can I pull all data for a certain rep, where the first two characters of the field equals BS?

推荐答案

在某些数据库中,您实际上可以做到

In some databases you can actually do

select * from SalesRep order by substring(SalesRepID, 1, 2)

其他人要求您

select *, Substring(SalesRepID, 1, 2) as foo from SalesRep order by foo

在其他情况下,您根本无法做到(但是从数据库中获取输出后,必须将其按程序代码进行排序).

And in still others, you can't do it at all (but will have to sort your output in program code after you get it from the database).

添加:如果您实际上只想要一位销售代表的数据,请按照其他人的建议进行操作.否则,您要么要sort by事物,要么可能是group by事物.

Addition: If you actually want just the data for one sales rep, do as the others suggest. Otherwise, either you want to sort by the thing or maybe group by the thing.

这篇关于按字段的前两个字符对SQL排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆