String Operators

I am going to press on with my series of very short articles or tidbits on Transaction SQL Operators. An operator is a symbol specifying an action that is performed on one or more expressions.

I will exploring the String Operators today. These operators can be broken down into two categories: concatenation and pattern matching.

Out of the two operators, pattern matching is the most complex.

Transaction SQL has both a simple and compound version of the concatenation operator. This operator takes two strings and combines them into one.

Please note that the compound version of the operator was introduced in later versions of the database engine. In addition, assignment when declaring a variable on a single line was also introduced later.

The two examples below return the same output.

The [] operator matches any single character within the specified range or set that is specified between the brackets. The example below returns all employees with id’s like 22#.

The _ operator matches any single character in a string comparison operation that involves pattern matching such as LIKE and PATINDEX. We have the same 10 records as output.

The % operator matches any string of zero or more characters. The output of this example adds one more record to the result set.

Last but not least, the ^ operator returns any strings that do not match this character. In the example below, we want to return all employees that have a first name that does not start with J. The result set has 256 matches. Shown below are the first five records.

In summary, concatenation operator + was defined as part of the TSQL syntax long before the CONCAT function was ever introduced. You are supplied with 4 pattern matching functions to slice and dice your data. I am sure you will come across string operators in your scripting endeavors.

Later today, I will be researching Unary Operators.

Related posts

Leave a Comment