SQL Server LIKE With Percent Literal


The LIKE operator in SQL Server is a really powerful tool. It allows you to specify a pattern to match records in your where clause. You can use the % (percent) as a wildcard value. For example: ‘monk%’ will match ‘monkey’ and ‘monkeys’. But what if you want to match the words ‘60% off sale’ and ‘60% off sales’… you can’t just put ‘60% off sale%’… you need to escape the first %. SQL Server gives us two different ways to escape a special character.

SQL Server LIKE – Exact Single Character

The first way that you can do this is by specifying a specific single character in your pattern. This is done by wrapping the single character in [ ]. The character that you put inside the brackets will tell the system that that character must be found exactly as appears.

SQL Server LIKE – Escape Character (read at the bottom of this post to find out what an escape character is)

The second way that you can do this is by specifying an escape character. This is done by using the keyword ESCAPE after your pattern. The literal value of the wildcard character following the escape value will be used instead of the wildcard value. In the example below, we specify that ! is our ESCAPE character in our string. Then we put ! before %. That way the database will look for the literal value of % instead of using that in the wildcard pattern.

What Is An Escape Character

An escape character is a character that is placed before a character (or string of characters) and it tells the system to read the following character as their literal value. Many systems have reserved characters that act as codes or wildcards in their system. Using an escape character, you tell the system to not read those values as special codes or wildcards.

Reference: https://msdn.microsoft.com/en-us/library/aa933232(v=sql.80).aspx

If Statement In An Update


If you are updating multiple rows in one statement, but only want to update a value in a column if a condition is satisfied, then you need to perform some sort of if logic inside your update. Here’s a straight forward way of doing this.

In the example below, I am just going to use 2 temp table variables. I’ll update the @SourceTable with the data from the @UpdateTable… but only where the UpdateFlag field is set to 1.

In the example above, you can see that the AnimalName is only updated when the UpdateFlag column is set to 1. The 2nd record “Cat” is not updated with the value “Cow” because it has a 0 for the UpdateFlag. This is done using a case statement on the SET operation. The example above is very simple. This case logic in the SET operation becomes much more helpful in more complex queries.

SQL Server Management Studio Dark Theme


If you like the dark theme of Visual Studio and wish that you had the dark theme for SQL Server Management Studio (SSMS), look no further.  Management Studio actually comes with a dark theme, but it just isn’t enabled.  It’s super simple to enable this feature.  Here’s how.

Note: I’ve confirmed this works with SQL Server 2016.

Here is what my current themed Management Studio looks like:
SQL Server Management Studio Dark Theme Light


The place in SQL Server Management Studio that we set the color theme is in the Options.  To see what we currently have, you go to the options by clicking Tools –> Options…
SQL Server Management Studio Dark Theme Options

From there we navigate to Environment –> General.  On the right side we can see Color theme:.  This option shows the current color theme that you are SSMS is using.
SQL Server Management Studio Dark Theme Options

My only options in the drop-down are Blue and Light:
SQL Server Management Studio Dark Theme Options

To allow the Dark theme to be selected, all we need to do is make a small change to the ssms.pkgundef file.  This file is located at: “C:\program files (x86)\Microsoft SQL Server\130\tools\binn\managementstudio\ssms.pkgundef”.


Step 1) Close SQL Server Management Studio and then browse to the ssms.pkgundef file and open it with Notepad.  The easiest way I found is to right click on the file and choose Edit.

TIP: If you can’t save the file after editing it, you may need to launch Notepad as administrator.

SQL Server Management Studio Dark Theme Edit

Step 2) Find the // Remove Dark theme key.  The easiest way is to just do a search in Notepad.
SQL Server Management Studio Dark Theme Edit

Step 3) Add 2 slashes (//) in front of this key that starts with [$RootKey$\Themes\.  Save and close the file.
SQL Server Management Studio Dark Theme Edit

Step 4) Relaunch SQL Server Management Studio.  Go back in to the options and select the Dark theme.
SQL Server Management Studio Dark Theme Done

This is what your final product should look like!  Enjoy!
SQL Server Management Studio Dark Theme Dark


SQL Server Minus Operator


The MINUS operator is an Oracle operator and does not exist in SQL Server. SQL Server has its own operator that does this called EXCEPT. The usage of these two operators are the same between Oracle and SQL Server.

The EXCEPT Operator
The EXCEPT operator will take two queries and only show the records in the first query that do not exist in the second query. Here is an example of how this would work.

The above query will show all the animals from the Animal table where the animals are not in the ExtinctAnimal table.

Reference: https://msdn.microsoft.com/en-us/library/ms188055.aspx



If you work with stored procedures or if you script objects in SQL Server, you have probably seen SET statements that turn on/off some crazy thing called ANSI_PADDING. If you’re like most people, you just ignore this because it works. I wanted to take a few minutes to explain what this ANSI_PADDING thing is.

ANSI_PADDING is an option that controls how VARCHAR and VARBINARY values are stored. If ANSI_PADDING is turned on, then SQL Server will NOT trim the trailing spaces when it inserts into a VARCHAR field. Similarly, it will NOT trim trailing nulls when it inserts into a VARBINARY field.

Setting the ANSI_PADDING only affects the spaces on inserts. It does NOT affect comparisons.

Syntax For Setting The ANSI_PADDING: