Advertisement
Guest User

Untitled

a guest
Aug 29th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.89 KB | None | 0 0
  1. --- Query to order and identify spoiler-ridden questions on anime.SE
  2.  
  3. DECLARE @qt_st INT;
  4. DECLARE @qt_cnt INT;
  5. DECLARE @qt_bdy NVARCHAR(MAX);
  6. DECLARE @cur_pst INT;
  7. DECLARE @to_search INT;
  8.  
  9. -- first pass stripping HTML tags, adapted from https://stackoverflow.com/a/457725/
  10. DECLARE @sta_string INT;
  11. DECLARE @end_string INT;
  12. DECLARE @len_string INT;
  13. DECLARE @stripped_body NVARCHAR(MAX);
  14.  
  15.  
  16. -- at first, all posts containing spoilers are heaped into a temporary table, to improve performance
  17. SELECT
  18. p.Id,
  19. p.Body,
  20. 1 AS sp_amnt,
  21. 1 AS bdy_amnt,
  22. p.CreationDate
  23.  
  24. INTO #temp_posts
  25.  
  26. FROM Posts AS p
  27.  
  28. WHERE ( CHARINDEX('<blockquote class="spoiler">', p.Body) > 0) AND ( p.PostTypeId = 1 );
  29.  
  30.  
  31. -- iterates across every single post in the temporary table
  32. -- second try using a cursor. Thanks to inspiration by @rene
  33.  
  34. DECLARE posts_cursor CURSOR
  35. FOR
  36. SELECT tmp.Id, tmp.Body, tmp.bdy_amnt, tmp.sp_amnt
  37. FROM #temp_posts AS tmp
  38. FOR UPDATE OF tmp.bdy_amnt, tmp.sp_amnt;
  39.  
  40. OPEN posts_cursor;
  41.  
  42. FETCH FIRST FROM posts_cursor
  43. INTO @cur_pst, @qt_bdy;
  44.  
  45.  
  46. WHILE @@FETCH_STATUS = 0
  47.   BEGIN
  48.     SET @qt_st = 1;
  49.     SET @qt_cnt = 0;
  50.     SET @to_search = 1;
  51.     WHILE @qt_st != 0 AND @qt_st IS NOT NULL
  52.       BEGIN
  53.             -- CHARINDEX returns the starting position of the
  54.             -- search string and the search string is 27 chars long
  55.             SET @qt_st = CHARINDEX('<blockquote class="spoiler">', @qt_bdy, @to_search) + 27;
  56.             SET @to_search = @to_search + @qt_st;
  57.         IF @to_search >= LEN(@qt_bdy)
  58.             SET @qt_st = 0;
  59.         IF @qt_st != 0 AND @qt_st IS NOT NULL
  60.           SET @qt_cnt = @qt_cnt + ( CHARINDEX('</blockquote>', @qt_bdy, @qt_st) - @qt_st );
  61.       END
  62.     -- first pass attempting to strip HTML tags before processing, adapted from https://stackoverflow.com/a/457725/
  63.     SET @stripped_body = @qt_bdy;
  64.     SET @sta_string = CHARINDEX('<',@stripped_body)
  65.     SET @end_string = CHARINDEX('>',@stripped_body,CHARINDEX('<',@stripped_body))
  66.     SET @len_string = (@end_string - @sta_string) + 1
  67.     WHILE @sta_string > 0 AND @end_string > 0 AND @len_string > 0
  68.       BEGIN
  69.           SET @stripped_body = STUFF(@stripped_body,@sta_string,@len_string,'');
  70.           SET @sta_string = CHARINDEX('<',@stripped_body);
  71.           SET @end_string = CHARINDEX('>',@stripped_body,CHARINDEX('<',@stripped_body));
  72.           SET @len_string = (@end_string - @sta_string) + 1;
  73.       END  
  74.     UPDATE tmp SET sp_amnt = @qt_cnt, bdy_amnt = LEN(LTRIM(RTRIM(@stripped_body))) WHERE CURRENT OF posts_cursor;
  75.     FETCH NEXT FROM posts_cursor
  76.     INTO @cur_pst, @qt_bdy;
  77.   END
  78.  
  79. CLOSE posts_cursor;
  80. DEALLOCATE posts_cursor;
  81.  
  82. SELECT
  83.  
  84. Id AS [Post Link],
  85. CreationDate AS [Post DATE],
  86. bdy_amnt AS [Lenght OF Post (chars)],
  87. sp_amnt AS [Spoiler Content (chars)],
  88. ( sp_amnt * 100 / bdy_amnt ) AS [Percent Spoiler]
  89.  
  90. FROM #temp_posts
  91.  
  92. ORDER BY ( sp_amnt * 100 / bdy_amnt ) DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement