Scalar Functions Flashcards
What is a function in a SQL statement?
A function accepts multiple input values like strings, numbers, dates, performs an action on them, and then returns a single value as a result.
What does CONCAT() do?
It takes multiple string values separated by commas and merges them together on a single line.
What does the Left() and Right() function do?
These functions list/extract characters from the string starting on the left or right side.
What information goes inside the parenthesis of the Left() and Right() function?
- Expression
- Length
LEFT(‘string’, number_of_characters)
EX: LEFT (Hello, World!’, 5)
LEFT(column_name, 5)
What is an identifier in SQL?
A name used to identify a database object like table, column, index, view, schema, database, schema names, alias names, view names, constraint names.
What does the function SUBSTRING() do?
It extracts a part of a string.
How does SUBSTRING() work?
You input a specific starting point and number of characters to extract from a string.
What is the SUBSTRING syntax?
SUBSTRING(string, starting position, length)
- String is the source we’re extracting from
- Start position is where the extraction would begin
- The number of characters to extract from the starting position.
Explain this section of the function LEN(product_code) - 4
SUBSTRING(product_code, 5, LEN(product_code) - 4)
LEN(product_code) - 4: The length of the substring.
LEN(product_code): This function returns the length of the product_code string. This subtracts 4 from the total length of the product_code string, determining how many characters to extract starting from the 5th character.
What are the three parameters the SUBSTRING() takes?
String, starting point, length to extract
What does the function LEN() do?
Displays the character count in a string. Spaces count as a character.
What does Ltrim() and Rtrim() do?
LTrim and RTrim
are functions
used to trim
leading or
trailing spaces
off of a
character
expression
How do you remove trailing spaces on both ends of a word?
LTRIM(RTRIM(‘string’))
What is the syntax for Ltrim() and Rtrim()?
RTRIM(‘string’)
What does the UPPER() and LOWER() function do?
Convert an expression to upper or lower case
What is the syntax of the UPPER() and LOWER() function?
It’s UPPER(‘string’) or LOWER(‘string’)
What does this expression do? UPPER(LEFT(column_name, 1))
IT extracts the first character of each word, UPPER() converts it to uppercase
What expression reverses the order of the characters in a string?
REVERSE(‘string’) or REVERSE(column_name)
Column named NAME
REVERSE(Name)
What parameters does the REVERSE() function use?
- Expression
- String of characters we’re searching for
- Replacement
string
Which function do you use if you want to search for a specific pattern (substring) within a string and return the starting position of the first occurrence of that pattern?
PATINDEX(‘%pattern%’, expression)
ex: PATINDEX(‘%world%’, ‘Hello world!’)
NOTE: It does not return the position right before, but the position of the first character.wil
What is the syntax of the REVERSE() function?
REPLACE(original_string, string_to_replace, replacement_string)
or
REPLACE (string or expression like a column we’re manipulating, ‘the part of the string we’re replacing’, ‘what we’re replacing it with’)
REPLACE(‘Hello World’, ‘World’, ‘Universe’)
‘World’ gets replaced with ‘Universe’
What does PATINDEX() share with the LIKE keyword?
It takes wildcards.
What happens if the string isn’t found with PATINDEX()?
The result is zero.
What parameters does the CHARINDEX() function take?
CHARINDEX(‘substring’, string or expression like the column, starting position (optional)
ex.
CHARINDEX(‘in’, Name, 8)
How is CHARINDEX different from PATINDEX?
CHARINDEX is useful for finding later instances of a character
pattern in an expression. You can skip over the first couple if you want to by identifying a starting point to start the function in the script.
CHARINDEX also doesn’t take wildcards.