Last active
June 10, 2025 20:58
-
-
Save mjswart-d2l/3fa24bf5a50fab039a707eeb3f620782 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DECLARE @StringAgg VARCHAR(1000); | |
| DECLARE @LastStation VARCHAR(50); | |
| SELECT @StringAgg = STRING_AGG(StationName, ''), | |
| @LastStation = MAX(StationName) | |
| FROM dbo.Stations; | |
| SELECT | |
| @StringAgg = STUFF( | |
| @StringAgg, | |
| CHARINDEX(StationFromName, @StringAgg) + 1, | |
| CHARINDEX(StationToName, @StringAgg) - CHARINDEX(StationFromName, @StringAgg) - 1, | |
| '') | |
| + SUBSTRING( | |
| @StringAgg, | |
| CHARINDEX(StationFromName, @StringAgg) + 1, | |
| CHARINDEX(StationToName, @StringAgg) - CHARINDEX(StationFromName, @StringAgg) - 1) | |
| FROM dbo.StationRoutingOverride | |
| SELECT SUBSTRING(@StringAgg, N.value, 1) AS StationName | |
| FROM generate_series(1, CHARINDEX(@LastStation, @StringAgg)) N | |
| /* | |
| StationName | |
| ----------- | |
| A | |
| B | |
| I | |
| D | |
| E | |
| S | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment