Chapter 16 - Substrings and Positioning Functions

“It’s always been and always will be the same in the world: the horse does the work and the coachman is tipped.”

- Anonymous

The TRIM Command trims both Leading and Trailing Spaces

Query 1

SELECT Last_Name

              ,Trim(Last_Name) AS No_Spaces

FROM    Employee_Table ;

Query 2

SELECT Last_Name

             ,Trim(Both from Last_Name) AS No_Spaces

FROM    Employee_Table ;

Both queries above do the exact same thing. They remove spaces from the beginning and the end of the column Last_Name.

Both queries trim both the leading and trailing spaces from Last_Name.

A Visual of the TRIM Command Using Concatenation

image

When you use the TRIM command on a column, that column will have all beginning and ending spaces removed.

Trim and Trailing is Case Sensitive

image

For leading and trailing TRIM commands, case sensitivity is important.

image

For LEADING and TRAILNG TRIM commands, case sensitivity is required.

How to TRIM Trailing Letters

image

The above example removed the trailing ‘y’ from the First_Name and the trailing ‘g’ from the Last_Name. Remember that this is case sensitive.

The SUBSTRING Command

image

First_Name

 

Quiz

Squiggy

 

qui

John

 

ohn

Richard

 

ich

Herbert

 

erb

Mandee

 

and

Cletus

 

let

William

 

ill

Billy

 

ill

Loraine

 

ora

This is a SUBSTRING. The substring is passed two parameters, and they are the starting position of the string and the number of positions to return (from the starting position). The above example will start in position 2 and go for 3 positions!

How SUBSTRING Works with NO ENDING POSITION

image

First_Name  

 

GoToEnd

Squiggy

 

quiggy

John

 

ohn

Richard

 

ichard

Herbert

 

erbert

Mandee

 

andee

Cletus

 

letus

William

 

illiam

Billy

 

illy

Loraine

 

oraine

If you don’t tell the Substring the end position, it will go all the way to the end.

Using SUBSTRING to move Backwards

image

First_Name

 

Before1

Squiggy

 

Squig

John

 

John

Richard

 

Richa

Herbert

 

Herbe

Mandee

 

Mande

Cletus

 

Cletu

William

 

Willi

Billy

 

Billy

Loraine

 

Lorai

A starting position of zero moves one space in front of the beginning. Notice that our FOR Length is 6 so ‘Squiggy’ turns into ‘ Squig’. The point being made here is that both the starting position and ending positions can move backwards which will come in handy as you see other examples.

How SUBSTRING Works with a Starting Position of -1

image

First_Name  

 

Before2   

Squiggy

 

S

John

 

J

Richard

 

R

Herbert

 

H

Mandee

 

M

Cletus

 

C

William

 

W

Billy

 

B

Loraine

 

L

A starting position of -1 moves two spaces in front of the beginning. Notice that our FOR Length is 3, so each name delivers only the first initial. The point being made here is that both the starting position and ending positions can move backwards which will come in handy as you see other examples.

How SUBSTRING Works with an Ending Position of 0

image

First_Name

 

WhatsUp

Squiggy

 

 

John

 

 

Richard

 

 

Herbert

 

 

Mandee

 

 

Cletus

 

 

William

 

 

Billy

 

 

Loraine

 

 

In our example above, we start in position 3, but we go for zero positions, so nothing is delivered in the column. That is what’s up!

The POSITION Command finds a Letters Position

SELECT Last_Name

               ,Position ('e' in Last_Name) AS Find_The_E

               ,Position ('f' in Last_Name) AS Find_The_F

FROM    Employee_Table ;

image

This is the position counter. What it will do is tell you what position a letter is on. Why did Jones have a 4 in the result set? The ‘e’ was in the 4th position. Why did Smith get a zero for both columns? There is no ‘e’ in Smith and no ‘f’ in Smith. If there are two ‘f’s, only the first occurrence is reported.

Quiz – Find that SUBSTRING Starting Position

SELECT DISTINCT Department_Name as Dept_Name

‚SUBSTRING(Department_Name FROM

    POSITION(' ' IN Department_Name) +1) as Word2

FROM Department_Table

WHERE POSITION(' ' IN trim(Department_Name)) >0;

Dept_Name

 

Word2

Customer Support

 

Support

Human Resources

 

Resources

Research and Develop

 

and Develop

What is the Starting Position here?

What is the Starting position of the Substring in the above query? Hint: This only looks for a Dept_Name that has two words or more.

Answer to Quiz – Find that SUBSTRING Starting Position

SELECT DISTINCT Department_Name as Dept_Name

‚SUBSTRING(Department_Name FROM

POSITION(' ' IN Department_Name) +1) as Word2

FROM Department_Table

WHERE POSITION(' ' IN trim(Department_Name)) >0;

Dept_Name

 

Word2

Customer Support

 

Support

Human Resources

 

Resources

Research and Develop

 

and Develop

What is the Starting Position here?

The Starting Position is calculated by finding the length up to the first SPACE and then adding 1.

Customer Support (FROM 10)

Human Resources (FROM 7)

Research and Develop FROM 10)

What is the Starting position of the Substring in the above query? See above!

Using the SUBSTRING to Find the Second Word On

SELECT DISTINCT Department_Name as Dept_Name

‚SUBSTRING(Department_Name FROM

POSITION(' ' IN Department_Name) +1) as Word2

FROM Department_Table

WHERE POSITION(' ' IN trim(Department_Name)) >0;

Dept_Name

 

Word2

Customer Support

 

Support

Human Resources

 

Resources

Research and Develop

 

and Develop

Notice we only had three rows come back. That is because our WHERE looks for only Department_Name that has multiple words. Then, notice that our starting position of the Substring is a subquery that looks for the first space. Then, it adds 1 to the starting position, and we have a staring position for the 2nd word. We don’t give a FOR length parameter, so it goes to the end.

Quiz – Why Did only one Row Return

SELECT Department_Name

‚SUBSTRING(Department_Name from

POSITION(' ' IN Department_Name) + 1 +

POSITION(' ' IN SUBSTRING(Department_Name

FROM POSITION(' ' IN Department_Name) + 1))) as Third_Word

FROM Department_Table

WHERE POSITION(' ' IN

TRIM(Substring(Department_Name from

POSITION(' ' in Department_Name) + 1)))> 0

Dept_Name

 

Third_Word

Research and Develop

 

Develop

Why did only one row come back?

Answer to Quiz – Why Did only one Row Return

SELECT Department_Name

‚SUBSTRING(Department_Name from

POSITION(' ' IN Department_Name) + 1 +

POSITION(' ' IN SUBSTRING(Department_Name

FROM POSITION(' ' IN Department_Name) + 1))) as Third_Word

FROM Department_Table

WHERE POSITION(' ' IN

TRIM(Substring(Department_Name from

POSITION(' ' in Department_Name) + 1)))> 0

Dept_Name

 

Third_Word

Research and Develop

 

Develop

It has 3 words

Why did only one row come back? It’s the Only Department Name with three words. The SUBSTRING and the WHERE clause both look for the first space, and if they find it, they look for the second space. If they find that, add 1 to it, and their Starting Position is the third word. There is no FOR position, so it defaults to “go to the end”.

Concatenation

image

See those || ? Those represent concatenation. That allows you to combine multiple columns into one column. The || (Pipe Symbol) on your keyboard is just above the ENTER key. Don’t put a space in between, but just put two Pipe Symbols together. In this example, we have combined the first name, then a single space, and then the last name to get a new column called ‘Full name’ like Squiggy Jones.

Concatenation and SUBSTRING

image

Of the three items being concatenated together, what is the first item of concatenation in the example above? The first initial of the First_Name. Then, we concatenated a literal space and a period. Then, we concatenated the Last_Name.

Four Concatenations Together

image

Why did we TRIM the Last_Name? To get rid of the spaces or the output would have looked odd. How many items are being concatenated in the example above? There are 4 items concatenated. We start with the Last_Name (after we trim it), then we have a single space, then we have the First Initial of the First Name, and then we have a Period.

Troubleshooting Concatenation

image

What happened above to cause the error. Can you see it? The Pipe Symbols || have a space between them like | |, when it should be ||. It is a tough one to spot, so be careful.

Declaring a Cursor

image

The above example declares a cursor named TeraTom to select sales information from the Sales_Table and then fetch rows from the result set using the cursor.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset