Section 7 - The World of String Functions Flashcards
Name 3 things we can do with String Functions.
- Join two pieces of data together
- Reverse a string
- Replace all spaces in a string with a particular character
- Conjoin a first and last name
- Combine two pieces of data together
What is a better method of writing (permanent) code than writing it directly in the console? Why?
Writing it in a file and run code from that file.
- It’s much easier to update and to modify!
- The formatting is a lot better!
- There is nice syntax highlighting!
- We can run them when we’re ready, not just instantly.
- Files are better for projects and more serious needs. Files allow us to share much more easily.
What’s the magic syntax for running a file?
source.sql
What does the source.sql command do?
This runs the mentioned file.
Why does it matter what directory you are in when you initialize the mysql cli?
- If the file you’re running is in the parent directory of the file you’re in, you’ll need to navigate to the whole file path.
- Otherwise, if not the above, MySQL will just look in the folder you’re currently in, and won’t be able to find the file.
What does the
ls
command do?
This checks the files in your current directory.
What command checks the files in your current directory?
ls
Is it a good idea to memorize all the string functions possible in MySQL? Why?
NO.
There’s a million for them, and you’ll likely only be using around 5 at a time.
What is the function for combining data?
CONCAT
What does CONCAT do?
It combines data.
TRUE or FALSE:
The CONCAT function combines data.
TRUE
CONCAT combines pieces of data (strings).
Is the above TRUE or FALSE?
TRUE
What is the correct syntax for using the CONCAT function?
SELECT CONCAT(‘This will create, ‘ ‘, ‘a full sentence.’);
Is this correct syntax?
CONCAT(‘Is this correct,’ ‘ ‘, ‘syntax?’);
Why or why not?
NO.
You can’t go around CONCAT-ing willy nilly. You need to let the system know what you’re selecting.
It should be written as:
SELECT CONCAT(‘Is this correct,’ ‘ ‘, ‘syntax?’);
Write a function that does the following:
Prints the author_fname + author_lname columns into a complete name, from a table named bookshop.
Make sure there’s a space between author_fname and author_lname.
SELECT CONCAT(author_fname, ‘ ‘, author_lname) FROM bookshop;
You can also format it this way, for easier readability:
SELECT
CONCAT(author_fname, ‘ ‘, author_lname)
FROM bookshop;
Does CONCAT-ing change the data? Or does it only change the way it’s printed out?
No, CONCAT does not change the data. It only changes the way it’s printed out.
Write a function that concatenates the president_fname and president_lname columns, and assigns them an alias named ‘Dead Presidents’, from a table called world_leaders.
SELECT CONCAT(president_fname, ‘ ‘, president_lname) AS ‘Dead Presidents’ FROM world_leaders;
Write the following function:
- Print column date_released to the left of a concatenated column featuring actor_firstinitial and actor_lastname. Actor_firstinitial and actor_lastname should be joined together by a hyphen.
- The table name is imdbd.
- Assign the alias “Released Date” to date_released and “Actor Name” to the concatenated actor first initial / actor last name column.
SELECT date_released AS ‘Released Date’,
CONCAT(actor_firstinitial, ‘-‘, actor_lastname) AS
‘Actor Name’
FROM imdbd;
What does the CONCAT_WS function do?
It concatenates the same symbol/letter/number/etc between each of your fields of data.
For example, you could use this to add a hyphen between each of 10+ different columns of data.
Write a CONCAT function that adds a hyphen between column names first_name, middle_name, and last_name.
The function should add both hyphens at once. You should NOT have to type two separate hyphens in your function.
The table name is middle_management_employees.
Hint: Use a particular CONCAT function.
SELECT CONCAT_WS(‘-‘, first_name, middle_name, last_name) FROM middle_management_employees;
In the CONCAT_WS function, where do you put the character/text/string/symbol that you want repeated before other symbols?
Give an example in practice.
You put it first in the lineup. For example:
SELECT CONCAT_WS(‘firstinlineup’, ‘first name’, ‘middle name’, ‘last name’);
The ‘firstinlineup’ text would be inserted between first name and middle name, and between middle and last name.
When using the CONCAT_WS function, does the function insert the repeated symbol/character/string/etc before and after the other data, or only between?
It only inserts it between.
For example:
SELECT CONCAT_WS(‘$’, ‘first name’, ‘last name’);
The above would print as “first name$last name”.
It would NOT print as $first name$last name$.
What does SUBSTRING do?
It allows us to work with part of a string.
What is the function for working with only part of a string?
SUBSTRING
Name a few things the function SUBSTRING could help us print.
- The first character of a string
- The middle 10 characters of the string
- The last letter of the string
Where do MySQL’s indices start? At what character?
MySQL’s indices start at 1 (one) NOT 0 (zero) as many programming languages do.
TRUE or FALSE:
MySQL’s indices start at 1.
TRUE
TRUE or FALSE:
MYSQL’s indices start at zero.
FALSE