Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using string functions

From: SQL Server 2008 Essential Training

Video: Using string functions

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.

Using string functions

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.

Show transcript

This video is part of

Image for SQL Server 2008 Essential Training
SQL Server 2008 Essential Training

74 video lessons · 36712 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 2m 21s
    1. Welcome
      1m 19s
    2. Using the exercise files
      1m 2s
  2. 17m 58s
    1. SQL Server core concepts
      9m 4s
    2. SQL Server editions
      3m 8s
    3. Applications included with SQL Server
      5m 46s
  3. 26m 1s
    1. Preparing for installation
      3m 44s
    2. Creating service accounts
      2m 33s
    3. Installing SQL Server
      11m 42s
    4. Post-installation checks
      3m 9s
    5. Installing sample databases
      4m 53s
  4. 13m 35s
    1. Introduction to SQL Server Management Studio
      8m 7s
    2. Introduction to SQL Server Books Online
      3m 6s
    3. SQL Server system databases
      2m 22s
  5. 1h 26m
    1. Planning your database
      9m 39s
    2. Creating a SQL Server database
      4m 7s
    3. Creating tables
      7m 51s
    4. Data types in SQL Server
      12m 25s
    5. Defining keys
      8m 9s
    6. Creating default values
      4m 39s
    7. Creating check constraints
      2m 25s
    8. Creating unique constraints
      4m 34s
    9. Introduction to relationships and foreign keys
      9m 51s
    10. Creating relationships in SQL Server Management Studio
      8m 14s
    11. Database normalization
      11m 47s
    12. Creating computed columns
      3m 10s
  6. 23m 11s
    1. Using the SQL Server Import and Export Wizard
      3m 58s
    2. Importing Excel files into SQL Server
      6m 11s
    3. Importing CSV files into SQL Server
      5m 27s
    4. Importing Access databases into SQL Server
      7m 35s
  7. 55m 29s
    1. Introduction to Transact-SQL
      3m 43s
    2. Using SELECT statements
      7m 16s
    3. Changing the default database
      2m 21s
    4. Creating conditions in SQL
      8m 10s
    5. Sorting your output
      3m 23s
    6. Using aggregate functions
      7m 12s
    7. Finding unique values
      2m 14s
    8. Joining multiple tables together
      8m 0s
    9. Using subqueries
      9m 33s
    10. Viewing execution plans
      3m 37s
  8. 19m 36s
    1. Writing INSERT statements
      5m 47s
    2. Writing UPDATE statements
      4m 38s
    3. Writing DELETE statements
      2m 54s
    4. Using the OUTPUT clause to return inserted keys and GUIDs
      6m 17s
  9. 32m 52s
    1. Introduction to SQL functions
      6m 26s
    2. Using SQL configuration functions
      2m 14s
    3. Using string functions
      7m 26s
    4. Using date functions
      6m 27s
    5. Creating user-defined functions
      10m 19s
  10. 28m 46s
    1. Introduction to stored procedures
      4m 23s
    2. Creating stored procedures
      11m 23s
    3. Introducing transactions
      4m 23s
    4. Creating transactions
      8m 37s
  11. 16m 39s
    1. Understanding and creating indexes
      6m 32s
    2. Monitoring and rebuilding indexes
      6m 0s
    3. Monitoring database size and integrity
      4m 7s
  12. 11m 41s
    1. Creating backups
      4m 21s
    2. Creating differential backups and using backup compression
      3m 40s
    3. Restoring databases
      3m 40s
  13. 17m 40s
    1. Introduction to SQL Server security and permissions
      5m 54s
    2. Adding a Windows user to the database
      5m 7s
    3. Creating SQL Server logins and switching authentication modes
      6m 39s
  14. 36m 41s
    1. Introduction to SQL Server Reporting Services
      2m 52s
    2. Connecting to the Report Manager
      4m 29s
    3. Using Report Builder
      12m 4s
    4. Formatting values in reports
      4m 17s
    5. Adding indicators to reports
      5m 11s
    6. Adding charts to reports
      3m 54s
    7. Working with report security
      3m 54s
  15. 24m 41s
    1. Introduction to SQL Server Integration Services (SSIS)
      1m 57s
    2. Using Business Intelligence Development Studio (BIDS)
      6m 59s
    3. Creating and executing a simple SSIS package
      7m 35s
    4. Importing packages into SQL Server Management Studio
      3m 21s
    5. Scheduling jobs with SQL Server Agent
      4m 49s
  16. 31s
    1. Goodbye
      31s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed SQL Server 2008 Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.