SQL 201 – String Manipulation Functions
In a previous post I introduced you to calculated fields, and mentioned there were many other functions you could use to compute values or columns. It’s time to introduce you to the eight most used functions when it comes to manipulating strings in SQL
LEFT and RIGHT
If you ever need the first 3 or last 3 characters of a string, then you’ll need to use LEFT or RIGHT They both have similar syntax.
LEFT(string, length) RIGHT(string, length)
The string can be a variable containing a VARCHAR or NVARCHAR, a literal VARCHAR or NVARCHAR, or a column with a type of VARCHAR or NVARCHAR. Are you sensing a pattern here? You can’t take the left 3 characters of an INT, or a DATETIME, or a TEXT field. It may seem obvious why you can’t do this operation on a number, but you may ask why you can’t use these functions on a TEXT field.
The short answer is TEXT values are a very special case to SQL. If you need to try to take the LEFT or RIGHT x characters of a number or text, first CAST or CONVERT that expression to a VARCHAR of sufficient size. Hopefully you can create a VARCHAR or NVARCHAR big enough to hold your string.
The length is an INTEGER telling the server how many characters you want, positive INTEGERS only please!
UPPER and LOWER
The syntax to UPPER and LOWER is simple too. UPPER changes all the characters to uppercase, and LOWER returns all lower case. You don’t have to use these functions in comparisons. Every SQL server I’ve used used a case insensitive collation for columns I searched through. What this means is A is equal to a. Also note, these functions only work on CHAR, NCHAR, VARCHAR, and NVARCHAR datatypes.
LTRIM and RTRIM
Whenever you deal with data that has come from free form fields, users will invariably add crap to the form. It’s not always intentional, but you as the Database Professional will have to clean it up. One of the most common items you have to clean up is leading or trailing spaces. The only odd thing is, Microsoft hasn’t built a method to do both at the same time. But I can show you how, but I’ll leave that for another post.
The syntax is simple.
You pass it a string (CHAR, NCHAR, VARCHAR, NVARCHAR), and it takes off either the leading spaces, or the trailing spaces.
If you need to do the opposite of LTRIM or RTRIM and repeat a character a certain number of times, such as adding blank characters onto the end of a string. Then REPLICATE is the function you’ll need.
The string is a VARCHAR, or something implicitly CONVERTable to a VARCHAR. This means SQL has to know how to turn the string you pass into a VARCHAR, otherwise you’ll get an error. The length is a positive INTEGER telling SQL how many of the character you want returned.
The last function I’ll show you is REPLACE. While this may be the most complex function in this post, it will become one of your most used functions.
REPLACE(searchedString, seekString, replaceWithString)
Basically you first pass the string you’re looking through to find the seekString. If SQL finds the seekString, it will replace that with the replaceWithString. This is great for more advanced lookups. Especially when you couple it with the wildcard searches I showed you in a past post. You can do some pretty impressive things.
These are only 8 of the functions SQL understands. In the future I’ll show you how to use more of these functions. Even later, I’ll show you how to create your own functions and do even more cool stuff!
If you have any questions, send them in! I’m always here to help!
|Previous: Calculated Fields||Next: Date Manipulation Functions|