Learn SQL: Microsoft SQL Server - Episode 12: Finding the length of a String
Tags: beginners, sql, database, programming
Published: 2021-12-18 07:35:20


![red-puppy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06tfwqtkcxfxd0kuqmee.gif)
Many times we have to look at how many characters there are in a string. Sometimes we even need to find the position of a character or a set of characters in a particular string in a column.


![spread-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/isa66rjq0sym1ekmirfz.png)

In this episode, we will look at how we can accomplish this very exercise.

In <abbr title="SQL Server Management Studio">SMSS</abbr>, for this example we will be using the **Person.EmailAddress** table. We will start by writing a SQL statement to **SELECT** everything from this table.


![skid-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdpva4fnapab6xc9ig4c.gif)

>We do this to get familiar with the columns and what data exist inside of them.

![select-person-emailaddress-table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/puz7nb8i1t75hrqwfco5.gif)

We will be using the **EmailAddress** column in our SQL statement. The first thing we want to do is _count_ the number of characters in the **EmailAddress** column for each row. To accomplish this we need to modify our SQL statement.


![spread-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eokp2orfw4ldwp3mvj8j.png)

####LEN

We will first be replacing the * with **EmailAddress**, this is to see which email address we are working with. We calculate the **length** or the number of characters by using the **LEN** function. **LEN** measures the length of a column.

![skid-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/36r8g0zzsbdp6vd8i94r.gif)

Lets see this in practice in <abbr title="SQL Server Management Studio">SMSS</abbr>.

![select-len-statement](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/trlz6ufv288vtuk8ro0f.gif)

This sort of function comes in handy, when we need to determine if there are any empty columns, or to extract a certain number of characters from a column. 

![spread-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eokp2orfw4ldwp3mvj8j.png)

####CHARINDEX

Next we are going to use a another function called **CharIndex**. The **CharIndex** function allows you to find the _position/index_ of a given character or a set of characters.

![skid-3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwqk9kcdgh2bbukbxqlm.gif)

The **CharIndex** function takes two parameters or arguments, the <u>first argument</u> is the character or set of characters you hope to find; this is written inbetween quotation marks, since it will most likely be a string. The <u>second argument</u> is the **column-name** where you want to look for this value.

```
CharIndex(firstArg, secondArg)
```
![skid-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdpva4fnapab6xc9ig4c.gif)

>Remember, to enclose the value of the first argument in single quotation marks.

To make it easier to identify we give the **CharIndex** query an **Alias**. In this example we will call it `@ Position`.

![find the position of a character](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnrqjguumy88kngqnkhj.gif)

There you have it, two new functions you can use to find the **LEN**gth of a set of characters in a column and **CharIndex** to find the <u>position</u> or <u>index</u> of a particular character or set of characters in a particular column.

![spread-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/isa66rjq0sym1ekmirfz.png)

😯 Now you have more tools added to your toolbelt 😯, don't forget to play around with them and think of some scenario's where they might be useful.

I hope you enjoyed this episode and get excited about learning SQL.

![yoga](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1n8ino79s1tme4x16l9h.gif)