SQL select records in special order
The plain SQL statement “SELECT * FROM mrcdct16” returns:
RETRN SEQNO RECRD FIELD
2,710 6 ORDDETF ORDNUM
100 5 INCIDENTR EMP#
102 7 ORDDETHISR ORDNUM
106 7 ORDERHEADR CUSNO
101 5 INCIDENTR EMP#
1 4 INCIDENTR EMP#
2,740 7 ORDDETHISR ORDNUM
103 5 INCIDENTR EMP#
105 5 INCIDENTR EMP#
6 7 ORDERHEADR CUSNO
107 7 ORDERHEADR CUSNO
We want to select records with the first letter in FIELD to be E, C, O. We can use SQL
SELECT * FROM mrcdct16 order by position (substring(field, 1, 1) in 'ECO')
100 5 INCIDENTR EMP# 2
101 5 INCIDENTR EMP# 2
1 4 INCIDENTR EMP# 2
103 5 INCIDENTR EMP# 2
105 5 INCIDENTR EMP# 2
106 7 ORDERHEADR CUSNO 2
6 7 ORDERHEADR CUSNO 2
107 7 ORDERHEADR CUSNO 2
710 6 ORDDETF ORDNUM 2
750 6 ORDDETF ORDNUM 2
Or select them with 2 letters in FIELD as EM,OR,CU
SELECT * FROM mrcdct16 order by position (substring(field, 1, 2) in 'EMORCU')
Then we get:
100 5 INCIDENTR EMP# 2
101 5 INCIDENTR EMP# 2
1 4 INCIDENTR EMP# 2
103 5 INCIDENTR EMP# 2
105 5 INCIDENTR EMP# 2
2,710 6 ORDDETF ORDNUM 2
2,750 6 ORDDETF ORDNUM 2
106 7 ORDERHEADR CUSNO 2
6 7 ORDERHEADR CUSNO 2
107 7 ORDERHEADR CUSNO 2
0 Comments:
Post a Comment
<< Home