HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

CodeLess Questions query

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
questionsquerycodeless

Problem

As part of an exercise by Jamal to clean up old questions on Code Review that have no code in them, I helped by writing this query on Stack Exchange Data Explorer: CodeLess Questions

select Id as [Post Link], CreationDate
from Posts
where PostTypeId = 1 --questions
  and Body not like '%

%'
and ClosedDate is null
order by CreationDate

I realize this is a relatively simple query, but I am always looking for ways to avoid like conditions (and not like), and this query irks me.

Note that the like condition is %% - this is to trap the automatically managed code blocks that Stack Exchange uses to identify code. I cannot easily trap the open-tags because sometimes, when a custom syntax is used, the open tags are hard to search on.

I am looking for insights on whether the query can be improved or whether there are alternatives to the systems I have used for identifying code-less questions.

For reference, when Stack Exchange gets a question, the markup is converted to HTML and stored in the Body column.

An example is as follows:

MarkDown:

I didn't write this but I noticed it in our codebase at work.

    var data = object && object.attribute && object.attribute.data ? object.attribute.data : {};

I get that the programmer is trying to check that the variable
`object.attribute.data` is defined before using it, but is seems like
so much repeated code. What's the best way to assign `data` the value
of `object.attribute.data` if it's defined, otherwise giving it a
value of `{}`?


HTML Encoding:

I didn't write this but I noticed it in our codebase at work.

var data = object && object.attribute && object.attribute.data ? object.attribute.data : {};


I get that the programmer is trying to check that the variable object.attribute.datais defined before using it, but is
seems like so much repeated code. What's the best way to assign
data the value of object.attribute.data if
it's defined, otherwise giving it a value of {}?

Solution

I have never used it before but I think what you are looking for is CHARINDEX I found a StackOverflow Answer that has several upvotes and no downvotes using it like this

WHERE CHARINDEX('

', Body) = 0

So far from what I have seen they are relatively close to the same speed, and is giving the same results as your query.

I would much rather read this than Body not like '%%'

I think we have found a new "hammer", let's find some nails.

Read more about CHARINDEX Here

There is also a CONTAINS function in T-SQL, but unfortunately it cannot be used on that column of that table because the column is not Full-Text Indexed

Read more about CONTAINS Here

Code Snippets

WHERE CHARINDEX('</code></pre>', Body) = 0

Context

StackExchange Code Review Q#93032, answer score: 7

Revisions (0)

No revisions yet.