# Untitled

a guest
Jul 4th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. SELECT
2.   (CONVERT(VARCHAR(1000), CASE
3.     WHEN SUBSTRING(CASE
4.         WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
5.         ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
6.       END, (1), (4)) = '(09)' OR
7.       SUBSTRING(CASE
8.         WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
9.         ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
10.       END, (1), (4)) = '(08)' OR
11.       SUBSTRING(CASE
12.         WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
13.         ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
14.       END, (1), (4)) = '(07)' OR
15.       SUBSTRING(CASE
16.         WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
17.         ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
18.       END, (1), (4)) = '(06)' OR
19.       SUBSTRING(CASE
20.         WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
21.         ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
22.       END, (1), (4)) = '(05)' OR
23.       SUBSTRING(CASE
24.         WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
25.         ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
26.       END, (1), (4)) = '(04)' OR
27.       SUBSTRING(CASE
28.         WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
29.         ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
30.       END, (1), (4)) = '(03)' OR
31.       SUBSTRING(CASE
32.         WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
33.         ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
34.       END, (1), (4)) = '(02)' OR
35.       SUBSTRING(CASE
36.         WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
37.         ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
38.       END, (1), (4)) = '(01)' OR
39.       SUBSTRING(CASE
40.         WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
41.         ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
42.       END, (1), (4)) = '(00)' THEN SUBSTRING(CASE
43.         WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
44.         ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
45.       END, (5), LEN(CASE
46.         WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
47.         ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
48.       END) - (4))
49.     ELSE CASE
50.         WHEN SUBSTRING(CASE
51.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
52.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
53.           END, (1), (3)) = '(Z)' OR
54.           SUBSTRING(CASE
55.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
56.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
57.           END, (1), (3)) = '(Y)' OR
58.           SUBSTRING(CASE
59.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
60.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
61.           END, (1), (3)) = '(X)' OR
62.           SUBSTRING(CASE
63.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
64.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
65.           END, (1), (3)) = '(W)' OR
66.           SUBSTRING(CASE
67.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
68.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
69.           END, (1), (3)) = '(V)' OR
70.           SUBSTRING(CASE
71.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
72.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
73.           END, (1), (3)) = '(U)' OR
74.           SUBSTRING(CASE
75.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
76.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
77.           END, (1), (3)) = '(T)' OR
78.           SUBSTRING(CASE
79.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
80.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
81.           END, (1), (3)) = '(S)' OR
82.           SUBSTRING(CASE
83.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
84.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
85.           END, (1), (3)) = '(R)' OR
86.           SUBSTRING(CASE
87.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
88.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
89.           END, (1), (3)) = '(Q)' OR
90.           SUBSTRING(CASE
91.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
92.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
93.           END, (1), (3)) = '(P)' OR
94.           SUBSTRING(CASE
95.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
96.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
97.           END, (1), (3)) = '(O)' OR
98.           SUBSTRING(CASE
99.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
100.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
101.           END, (1), (3)) = '(N)' OR
102.           SUBSTRING(CASE
103.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
104.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
105.           END, (1), (3)) = '(M)' OR
106.           SUBSTRING(CASE
107.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
108.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
109.           END, (1), (3)) = '(L)' OR
110.           SUBSTRING(CASE
111.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
112.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
113.           END, (1), (3)) = '(K)' OR
114.           SUBSTRING(CASE
115.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
116.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
117.           END, (1), (3)) = '(J)' OR
118.           SUBSTRING(CASE
119.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
120.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
121.           END, (1), (3)) = '(I)' OR
122.           SUBSTRING(CASE
123.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
124.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
125.           END, (1), (3)) = '(H)' OR
126.           SUBSTRING(CASE
127.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
128.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
129.           END, (1), (3)) = '(G)' OR
130.           SUBSTRING(CASE
131.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
132.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
133.           END, (1), (3)) = '(F)' OR
134.           SUBSTRING(CASE
135.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
136.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
137.           END, (1), (3)) = '(E)' OR
138.           SUBSTRING(CASE
139.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
140.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
141.           END, (1), (3)) = '(D)' OR
142.           SUBSTRING(CASE
143.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
144.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
145.           END, (1), (3)) = '(C)' OR
146.           SUBSTRING(CASE
147.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
148.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
149.           END, (1), (3)) = '(B)' OR
150.           SUBSTRING(CASE
151.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
152.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
153.           END, (1), (3)) = '(A)' THEN SUBSTRING(CASE
154.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
155.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
156.           END, (4), LEN(CASE
157.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
158.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
159.           END) - (3))
160.         ELSE CASE
161.             WHEN CHARINDEX('  ', [#ResultSet].[Sector]) > (2) THEN RTRIM(LTRIM(SUBSTRING([#ResultSet].[Sector], (0), CHARINDEX('  ', [#ResultSet].[Sector]))))
162.             ELSE LTRIM(RTRIM([#ResultSet].[Sector]))
163.           END
164.       END
165.   END, 0))
166. FROM [#ResultSet];