Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Season 1, Season 2, Season 10, Season 20
- Season 1, Season 10, Season 2, Season 20
- create table tvseason
- (
- title varchar(100)
- );
- insert into tvseason (title)
- values ('100 Season 03'), ('100 Season 1'),
- ('100 Season 10'), ('100 Season 2'),
- ('100 Season 4'), ('Show Season 1 (2008)'),
- ('Show Season 2 (2008)'), ('Show Season 10 (2008)'),
- ('Another Season 01'), ('Another Season 02'),
- ('Another 1st Anniversary Season 01'),
- ('Another 2nd Anniversary Season 01'),
- ('Another 10th Anniversary Season 01'),
- ('Some Show Another No Season Number'),
- ('Some Show No Season Number'),
- ('Show 2 Season 1'),
- ('Some Show With Season Number 1'),
- ('Some Show With Season Number 2'),
- ('Some Show With Season Number 10');
- select
- title, "index", titleLeft,
- convert(int, coalesce(nullif(titleRightTrim2, ''), titleRight)) titleRight
- from
- (select
- title, "index", titleLeft, titleRight, titleRightTrim1,
- case
- when PATINDEX('%[^0-9]%', titleRightTrim2) = 0
- then titleRightTrim2
- else left(titleRightTrim2, PATINDEX('%[^0-9]%', titleRightTrim2) - 1)
- end as titleRightTrim2
- from
- (select
- title,
- len(title) - PATINDEX('%[0-9] %', reverse(title)) 'index',
- left(title, len(title) - PATINDEX('%[0-9] %', reverse(title))) titleLeft,
- ltrim(right(title, PATINDEX('%[0-9] %', reverse(title)))) titleRight,
- ltrim(right(title, PATINDEX('%[0-9] %', reverse(title)))) titleRightTrim1,
- left(ltrim(right(title, PATINDEX('%[0-9] %', reverse(title)))), PATINDEX('% %', ltrim(right(title, PATINDEX('%[0-9] %', reverse(title)))))) titleRightTrim2
- from
- tvseason) x) y
- order by
- titleLeft, titleRight
- title
- 100 Season 1
- 100 Season 2
- 100 Season 03
- 100 Season 4
- 100 Season 10
- **Case 7 here**
- Another 10th Anniversary Season 01
- Another 1st Anniversary Season 01
- Another 2nd Anniversary Season 01
- Another Season 01
- Another Season 02
- Show (2008) Season 1
- Show (2008) Season 2
- Show 2 The 75th Anniversary Season 1
- Show Season 1 (2008)
- Show Season 2 (2008)
- Show Season 10 (2008)
- Some Show Another No Season Number
- Some Show No Season Number
- Some Show With Season Number 1
- Some Show With Season Number 2
- Some Show With Season Number 10
- if object_id('tempdb.dbo.#titles') is not null drop table #titles
- create table #titles (Title varchar(100))
- insert into #titles (TItle)
- select title = '100 Season 1'
- union all select '100 Season 2'
- union all select '100 Season 03'
- union all select '100 Season 4'
- union all select '100 Season 10'
- union all select 'Another 10th Anniversary Season 01'
- union all select 'Another 1st Anniversary Season 01'
- union all select 'Another 2nd Anniversary Season 01'
- union all select 'Another Season 01'
- union all select 'Another Season 02'
- union all select 'Show (2008) Season 1'
- union all select 'Show (2008) Season 2'
- union all select 'Show 2 The 75th Anniversary Season 1'
- union all select 'Show Season 1 (2008)'
- union all select 'Show Season 2 (2008)'
- union all select 'Show Season 10 (2008)'
- union all select 'Some Show Another No Season Number'
- union all select 'Some Show No Season Number'
- union all select 'Some Show With Season Number 1'
- union all select 'Some Show With Season Number 2'
- union all select 'Some Show With Season Number 10'
- ;with src as
- (
- select
- Title,
- Trimmed = case when Title like '%Season [0-9]%'
- then stuff(title, 1, patindex('%season [0-9]%', title) + 6, '')
- else null
- end
- from #titles
- )
- select
- Season = cast(case when Trimmed like '%[^0-9]%' then left(Trimmed, patindex('%[^0-9]%', Trimmed))
- else Trimmed
- end as int),
- Title
- from src
- order by Season
Add Comment
Please, Sign In to add comment