Guest User

Untitled

a guest
Mar 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. Season 1, Season 2, Season 10, Season 20
  2.  
  3. Season 1, Season 10, Season 2, Season 20
  4.  
  5. create table tvseason
  6. (
  7. title varchar(100)
  8. );
  9.  
  10. insert into tvseason (title)
  11. values ('100 Season 03'), ('100 Season 1'),
  12. ('100 Season 10'), ('100 Season 2'),
  13. ('100 Season 4'), ('Show Season 1 (2008)'),
  14. ('Show Season 2 (2008)'), ('Show Season 10 (2008)'),
  15. ('Another Season 01'), ('Another Season 02'),
  16. ('Another 1st Anniversary Season 01'),
  17. ('Another 2nd Anniversary Season 01'),
  18. ('Another 10th Anniversary Season 01'),
  19. ('Some Show Another No Season Number'),
  20. ('Some Show No Season Number'),
  21. ('Show 2 Season 1'),
  22. ('Some Show With Season Number 1'),
  23. ('Some Show With Season Number 2'),
  24. ('Some Show With Season Number 10');
  25.  
  26. select
  27. title, "index", titleLeft,
  28. convert(int, coalesce(nullif(titleRightTrim2, ''), titleRight)) titleRight
  29. from
  30. (select
  31. title, "index", titleLeft, titleRight, titleRightTrim1,
  32. case
  33. when PATINDEX('%[^0-9]%', titleRightTrim2) = 0
  34. then titleRightTrim2
  35. else left(titleRightTrim2, PATINDEX('%[^0-9]%', titleRightTrim2) - 1)
  36. end as titleRightTrim2
  37. from
  38. (select
  39. title,
  40. len(title) - PATINDEX('%[0-9] %', reverse(title)) 'index',
  41. left(title, len(title) - PATINDEX('%[0-9] %', reverse(title))) titleLeft,
  42. ltrim(right(title, PATINDEX('%[0-9] %', reverse(title)))) titleRight,
  43. ltrim(right(title, PATINDEX('%[0-9] %', reverse(title)))) titleRightTrim1,
  44. left(ltrim(right(title, PATINDEX('%[0-9] %', reverse(title)))), PATINDEX('% %', ltrim(right(title, PATINDEX('%[0-9] %', reverse(title)))))) titleRightTrim2
  45. from
  46. tvseason) x) y
  47. order by
  48. titleLeft, titleRight
  49.  
  50. title
  51. 100 Season 1
  52. 100 Season 2
  53. 100 Season 03
  54. 100 Season 4
  55. 100 Season 10
  56. **Case 7 here**
  57. Another 10th Anniversary Season 01
  58. Another 1st Anniversary Season 01
  59. Another 2nd Anniversary Season 01
  60. Another Season 01
  61. Another Season 02
  62. Show (2008) Season 1
  63. Show (2008) Season 2
  64. Show 2 The 75th Anniversary Season 1
  65. Show Season 1 (2008)
  66. Show Season 2 (2008)
  67. Show Season 10 (2008)
  68. Some Show Another No Season Number
  69. Some Show No Season Number
  70. Some Show With Season Number 1
  71. Some Show With Season Number 2
  72. Some Show With Season Number 10
  73.  
  74. if object_id('tempdb.dbo.#titles') is not null drop table #titles
  75. create table #titles (Title varchar(100))
  76. insert into #titles (TItle)
  77. select title = '100 Season 1'
  78. union all select '100 Season 2'
  79. union all select '100 Season 03'
  80. union all select '100 Season 4'
  81. union all select '100 Season 10'
  82. union all select 'Another 10th Anniversary Season 01'
  83. union all select 'Another 1st Anniversary Season 01'
  84. union all select 'Another 2nd Anniversary Season 01'
  85. union all select 'Another Season 01'
  86. union all select 'Another Season 02'
  87. union all select 'Show (2008) Season 1'
  88. union all select 'Show (2008) Season 2'
  89. union all select 'Show 2 The 75th Anniversary Season 1'
  90. union all select 'Show Season 1 (2008)'
  91. union all select 'Show Season 2 (2008)'
  92. union all select 'Show Season 10 (2008)'
  93. union all select 'Some Show Another No Season Number'
  94. union all select 'Some Show No Season Number'
  95. union all select 'Some Show With Season Number 1'
  96. union all select 'Some Show With Season Number 2'
  97. union all select 'Some Show With Season Number 10'
  98.  
  99. ;with src as
  100. (
  101. select
  102. Title,
  103. Trimmed = case when Title like '%Season [0-9]%'
  104. then stuff(title, 1, patindex('%season [0-9]%', title) + 6, '')
  105. else null
  106. end
  107. from #titles
  108. )
  109. select
  110. Season = cast(case when Trimmed like '%[^0-9]%' then left(Trimmed, patindex('%[^0-9]%', Trimmed))
  111. else Trimmed
  112. end as int),
  113. Title
  114. from src
  115. order by Season
Add Comment
Please, Sign In to add comment