CHAPTER 4: Using Built-in Functions and Expressions Flashcards
(242 cards)
Q: What operator is used to concatenate strings in T-SQL?
A: The + operator is used to concatenate strings.
Q: How can you concatenate columns in a query with a space in between?
SELECT FirstName + ‘ ‘ + LastName AS [Full Name] FROM Person.Person;
Q: What happens if you concatenate a string with a NULL using the + operator?
A: The result is NULL.
Q: Why is it important to alias concatenated expressions?
A: Without an alias, the resulting column header will be “(No column name),” making the results harder to interpret.
Q: What is the purpose of the CONCAT function in T-SQL?
A: The CONCAT function concatenates multiple values into a single string and automatically handles NULLs by ignoring them.
Q: How does the CONCAT function handle NULL values?
A: NULL values are ignored, and the function concatenates the remaining values.
Q: What is an example of using CONCAT with literal strings?
SELECT CONCAT(‘I ‘, ‘love’, ‘ T-SQL’) AS RESULT;
Q: Can the CONCAT function handle nonstring data types?
A: Yes, nonstring values are implicitly converted to strings before concatenation.
Q: What is an example of using CONCAT with variables?
DECLARE @a VARCHAR(30) = ‘My birthday is on ‘;
DECLARE @b DATE = ‘1980-08-25’;
SELECT CONCAT(@a, @b) AS RESULT;
Q: How can CONCAT be used with table data?
SELECT CONCAT(AddressLine1, PostalCode) AS Address FROM Person.Address;
Q: How does this query handle NULLs?
SELECT CONCAT(‘This’, NULL, ‘ works’) AS RESULT;
A: The result is “This works” because NULL values are ignored.
Q: Why might you prefer CONCAT over the + operator for string concatenation?
A: CONCAT handles NULL values gracefully by ignoring them, whereas + returns NULL if any value is NULL.
Q: What is the purpose of the ISNULL function in T-SQL?
A: The ISNULL function replaces NULL values with a specified replacement value.
Q: How does the COALESCE function differ from ISNULL?
A: COALESCE accepts multiple arguments and returns the first non-NULL value, while ISNULL accepts only two arguments.
Q: Provide an example of using ISNULL to handle NULLs in a concatenation.
SELECT FirstName + ISNULL(‘ ‘ + MiddleName, ‘’) + ‘ ‘ + LastName AS [Full Name]
FROM Person.Person;
Q: Why might developers prefer COALESCE over ISNULL?
A: COALESCE is ANSI-compliant, more versatile, and can handle multiple arguments, whereas ISNULL is a proprietary SQL Server feature.
Q: How does COALESCE work with NULL values in concatenation?
A: It skips NULL values and uses the first non-NULL value, e.g.:
SELECT FirstName + COALESCE(‘ ‘ + MiddleName, ‘’) + ‘ ‘ + LastName AS [Full Name]
FROM Person.Person;
Q: What issue can occur when using ISNULL incorrectly in concatenation?
A: Extra spaces may appear if the space is not included within the ISNULL function.
Q: What must you do to concatenate nonstring values to strings in T-SQL?
A: Convert the nonstring values to strings using CAST, CONVERT, or CONCAT.
Q: What happens if you attempt to concatenate a string and a number without converting the number?
A: An error will occur because integers have higher precedence than strings.
Q: What is the syntax for using the CAST function to convert a value to a string?
CAST(<value> AS <new>)</new></value>
Q: What is the syntax for using the CONVERT function to convert a value to a string?
CONVERT(<new>, <value>)</value></new>
Q: Provide an example of using CAST to convert and concatenate a value.
SELECT CAST(BusinessEntityID AS NVARCHAR) + ‘: ‘ + LastName + ‘, ‘ + FirstName AS ID_Name
FROM Person.Person;
Q: Provide an example of using CONVERT to convert and concatenate a value.
SELECT CONVERT(NVARCHAR(10), BusinessEntityID) + ‘: ‘ + LastName + ‘, ‘ + FirstName AS ID_Name
FROM Person.Person;