Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Every SQL Server database you are going to work with is likely to contain immense amounts of text in varchar and char columns. So it's no surprise that there are many string functions to deal with that text and let me go through a few of those. So I am going to use AdventureWorksLT just to have some columns to work with and play around with here. Let's say we just start off with a very simple SELECT statement, selecting the LastName FROM SalesLT.Customer. Now, this is no surprise. We are not using a function yet.
It's simply going to go against that table and fetch every row but just that one column. But we can use that information and feed that into any of those string functions. We could start with the simple ones. There're functions like UPPER. It shows up in hot pink here and just takes one argument. We feed in LastName into the UPPER function and it will return the result of that function, which in this case not surprisingly is just turning all of those LastNames into uppercase. Now do bear in mind this is not changing the data in the database.
It's performing this operation on the way out as we are reading it and just making it a convenient way to read through this. You might know that there's quite a few countries that actually quite like the presentation of a name to have the LastName uppercased. So what we could do is start to combine this. Perhaps say I want FirstName and then a space and then uppercase the LastName and I will call the result FullName from SalesLT.Customer. Execute that and we get this presentation returning.
So it's a nice way to add some flexibility to what your applications might want to do without changing the data that's actually in your tables. But that's only the beginning of what we can do with strings. There are other things that we can ask for. Let's say I'm curious about the maximum length of a FirstName. Well, I can get both the FirstNames and I can call the function L-E-N or LEN to be the length of that. Again, feeding in a character column to get that information back. Hit Execute and we are retrieving both the FirstNames and the length of the FirstNames as an integer.
One of the great things about using these functions is you can nest one inside the other. So if I have executed this and I found out that we've got a whole bunch of different values but I have got thousands of rows, well, what's the largest one? Well, we've seen already the aggregate function called MAX. We can use that here. What I'm going to do is just use the MAX function, open parenthesis, and pass in the LEN function inside of it. So now we'll execute the LEN part first. We will find the length of everything and then we will find the MAX of that.
Execute and we apparently find out that someone has a FirstName with 24 characters. Well, that's fairly long. Of course, this way of doing it isn't actually telling me who that is. Well, here's where we can actually take some of these functions and instead of just using them in the SELECT statement or the SELECT part, we can actually use them in a WHERE or in this case in an ORDER BY clause. Plus I want to select FirstName from SalesLT.Customer ORDER BY, not the FirstName descending, but the length of FirstName descending.
Execute and we find that indeed we have Janaina Barreiro Gambaro is apparently the longest one. That's quite a mouthful. As ever, you will find the Books Online is your friend here. You will see that there's a String Functions section where it talks about things like our UPPER, our LOWER, our LEN. There's ways of finding the LEFT, for example, which will turn the left-hand side of the character strings, say the first three characters or the first four characters. We have the ability to reverse them, to get things on the right.
If you're working with fixed length strings, you might look at LTRIM and RTRIM, which have the ability to trim off any trailing or leading spaces from that length. And the wonderfully titled STUFF allows you to insert one string into another string. You'll also find a few odd ones, which you may not even have thought existed in the database. Let me show you an example of using SOUNDEX. I am going to jump back over here into my SQL and what I am going to do is select FirstName, LastName and then call the function called SOUNDEX on LastName.
FROM SalesLT.Cutstomer. And what is this going to do? Well, it actually runs through an algorithm that figures out as much as it can. What the pronunciation, what the sound alike value is of these particular names. What will happen is you will get this four-character result for calling SOUNDEX that starts with the leading letter and then has a series of numbers kind of representing what it considers to be the overall sound of the remaining vowels and consonants.
But what does that mean? Well, let's take it bit by bit. What would be interesting is if we then decided to ORDER BY the SOUNDEX of the LastName. Now, not surprisingly, where we have got some duplicate data here, they are matching up. But if I go down a little bit, what I am going to find is certain parts will actually starts to turn up where we are getting the same result, the same SOUNDEX, for actual different LastNames. So in this case, it's actually giving us the estimate here that Barker and Berger have kind of the same sound alike ability.
So if we were trying to do a function or perhaps searching through customer names because we think that somebody's had the wrong name entered and we are not quite sure what it might've been spelled as, but we could do something like search for a particular customer WHERE the SOUNDEX LastName is equal to, and let's say their LastName is Brown. But we want to actually find that if it was possibly entered in under some other title. So if I execute that, what I am now actually getting is it's finding the overall sound of Brown and then matching that against anything else that sounds the same in the Customer table and returning that.
So in this case, we are getting Brian, Brown, Bruno. We might debate if they actually sound alike but you could certainly see how this could be useful, particularly if you are trying to do, say, customer lookup applications. Again as ever, Books Online is going to be something that you want to have bookmarked and favorited until you become familiar with the different string functions and pick your little favorites. So take a look in Books Online at those available string functions. You'll find that some of them you're likely to use all the time, others not so much.
But diving into each of the named functions is going to give you all the necessary information you need to know about how to use it. Most of the examples I showed just took one parameter. You will find a few of these functions such as SUBSTRING and STUFF will take more than one parameter because they're working with larger strings and they need to know where to start and where to finish. But other than that, these string functions kind of work the same way.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 74231 Viewers
80 Video lessons · 129577 Viewers
52 Video lessons · 63854 Viewers
59 Video lessons · 49643 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.