次の表があります。
==========================================================
| Name_Level_Class_Section | Phone Num |
==========================================================
| Jacky_1_B2_23 | 1122554455 |
| Johnhy_1_B2_24 | 1122554455 |
| Peter_2_A5_3 | 1122554455 |
==========================================================
SQLステートメントを次のように簡略化しようと考えています。
select
*,
substring(Name_Level_Class_Section,
CHARINDEX('_',Name_Level_Class_Section,
(CHARINDEX('_', Name_Level_Class_Section) + 1)) + 1,
CHARINDEX('_',Name_Level_Class_Section,
(CHARINDEX('_',Name_Level_Class_Section,
(CHARINDEX('_',Name_Level_Class_Section)+1))+1))-
CHARINDEX('_',Name_Level_Class_Section,
(CHARINDEX('_',Name_Level_Class_Section)+1))) as CLA
from
Bookings
order by
CLA asc, Name_Level_Class_Section asc
したがって、SQLを実行すると、次の結果が得られます。
==========================================================
| Name_Level_Class_Section | Phone Num | CLA |
==========================================================
| Jacky_1_B2_23 | 1122554455 | B2 |
| Johnhy_1_B2_24 | 1122554455 | B2 |
| Peter_2_A5_3 | 1122554455 | A5 |
==========================================================
SQLを単純化する方法はありますか?