patternsqlMajor
Get the 2nd or 3rd occurrence of a value in a delimited string
Viewed 0 times
thedelimited3rdvaluegetoccurrencestring2nd
Problem
I have the following table:
I am thinking of simplified my SQL statement as follows:
So that when I perform the SQL, it will give me the following result:
Is there any way to simplify my SQL?
==========================================================
| Name_Level_Class_Section | Phone Num |
==========================================================
| Jacky_1_B2_23 | 1122554455 |
| Johnhy_1_B2_24 | 1122554455 |
| Peter_2_A5_3 | 1122554455 |
==========================================================I am thinking of simplified my SQL statement as follows:
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 ascSo that when I perform the SQL, it will give me the following result:
==========================================================
| 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 |
==========================================================Is there any way to simplify my SQL?
Solution
You can use
Result:
Update: Using your table the query would look like this:
Update 2:
If you know for sure that your value never contains a period
cross apply and the third parameter of charindex to get the position of the underscores.declare @T table
(
Name_Level_Class_Section varchar(25)
)
insert into @T values
('Jacky_1_B2_23'),
('Johnhy_1_B2_24'),
('Peter_2_A5_3')
select substring(Name_Level_Class_Section, P2.Pos + 1, P3.Pos - P2.Pos - 1)
from @T
cross apply (select (charindex('_', Name_Level_Class_Section))) as P1(Pos)
cross apply (select (charindex('_', Name_Level_Class_Section, P1.Pos+1))) as P2(Pos)
cross apply (select (charindex('_', Name_Level_Class_Section, P2.Pos+1))) as P3(Pos)Result:
-------------------------
B2
B2
A5Update: Using your table the query would look like this:
select *,
substring(Name_Level_Class_Section, P2.Pos + 1, P3.Pos - P2.Pos - 1) as CLA
from Bookings
cross apply (select (charindex('_', Name_Level_Class_Section))) as P1(Pos)
cross apply (select (charindex('_', Name_Level_Class_Section, P1.Pos+1))) as P2(Pos)
cross apply (select (charindex('_', Name_Level_Class_Section, P2.Pos+1))) as P3(Pos)
order by CLA asc,
Name_Level_Class_Section ascUpdate 2:
If you know for sure that your value never contains a period
. and that it is always a four part name you can use parsename.select *,
parsename(replace(Name_Level_Class_Section, '_', '.'), 2) as CLA
from Bookings
order by CLA asc,
Name_Level_Class_Section ascCode Snippets
declare @T table
(
Name_Level_Class_Section varchar(25)
)
insert into @T values
('Jacky_1_B2_23'),
('Johnhy_1_B2_24'),
('Peter_2_A5_3')
select substring(Name_Level_Class_Section, P2.Pos + 1, P3.Pos - P2.Pos - 1)
from @T
cross apply (select (charindex('_', Name_Level_Class_Section))) as P1(Pos)
cross apply (select (charindex('_', Name_Level_Class_Section, P1.Pos+1))) as P2(Pos)
cross apply (select (charindex('_', Name_Level_Class_Section, P2.Pos+1))) as P3(Pos)-------------------------
B2
B2
A5select *,
substring(Name_Level_Class_Section, P2.Pos + 1, P3.Pos - P2.Pos - 1) as CLA
from Bookings
cross apply (select (charindex('_', Name_Level_Class_Section))) as P1(Pos)
cross apply (select (charindex('_', Name_Level_Class_Section, P1.Pos+1))) as P2(Pos)
cross apply (select (charindex('_', Name_Level_Class_Section, P2.Pos+1))) as P3(Pos)
order by CLA asc,
Name_Level_Class_Section ascselect *,
parsename(replace(Name_Level_Class_Section, '_', '.'), 2) as CLA
from Bookings
order by CLA asc,
Name_Level_Class_Section ascContext
StackExchange Database Administrators Q#42393, answer score: 27
Revisions (0)
No revisions yet.