20.1. SQL Part 2 - String Functions¶
20.1.1. String Functions¶
Function |
Definition |
Syntax |
Value Returned |
---|---|---|---|
Removes whitespaces from right of last character. |
|
Too many extra spaces. |
|
Removes whitespaces from left of first character. |
|
Leading spaces |
|
LEFT. |
Returns length of characters starting at provided character index and moving left. |
|
hello t |
Returns length of characters starting at provided character index and moving right. |
|
re! |
|
LEN. |
Returns the length of a string based on characters, not including trailing spaces. |
|
12 |
Returns the length of a string based on bytes, not including trailing spaces. |
|
12 |
|
Can use to find specific character within a string. Returns the index location. |
|
5 |
|
Returns part of a string. First number is starting index location and second number is ending index location. |
|
ings are |
|
Returns the string backwards. |
|
sisylanA ataD |
|
Returns a string either in all upper cases. |
|
TACO |
|
Returns a string either in all lower cases. |
|
taco |
|
Replaces part of a string using provided patterns. |
|
Beech Street |
|
In this example, the code replaces the ‘ea’ with ‘ee’ if the ‘ea’ pattern is present. |
|
Reed |
|
Combine strings together. |
|
AlyceCatFrey |
|
Good for working with null values as seen in example 2. |
|
AlyceFrey |
|
Combines strings together with a specified separator value. The separator can be anything you want. |
|
Alyce=::=Frey |
|
Works with NULL values like |
|
Alyce - Frey |
|
Inserts string into another string. The first number indicates where to insert the new chars. |
|
PuAdd Charsmpkin Pie |
|
The second number indicates how many original characters will be deleted upon insertion. |
|
PuDelete Chars Pie |
20.1.2. Check Your Understanding¶
Question
Alyce is working with a dataset that contains information about local businesses. They want to create a query that returns titles of local businesses that contain “&”. Which string function should they use?
LEN
STUFF
CHARINDEX
RTRIM
Question
Alyce wants to create consistency in the formatting of the state abbreviations of the local business addresses contained in their dataset. What string function could help with this?
REVERSE
UPPER
CONCAT
LEFT
Question
Alyce’s dataset breaks down the addresses of local businesses into the following columns: "Street"
, "City"
, "State"
, and "Zipcode"
. They would like to create a column that has all items joined to return a complete address in a single column. Which function would best help with this?
CONCAT
orCONCAT_WS
orSTUFF
LOWER
REPLACE
LEFT
Question
Alyce is working with a column of phone numbers. They only need the 7 digit number, not the area code. However, some of the numbers include the area code, some include the country code and area code, and some only include the seven-digit phone number. Which function could help them select the 7 digits that she needs?
CONCAT_WS
CHARINDEX
RIGHT
RTRIM