SQL Concat, Aggregate and Split Function
DECLARE @string1 NVARCHAR(100) = 'Chocolate with beans from';
DECLARE @string2 NVARCHAR(100) = 'has a cocoa percentage of';
SELECT
bean_type,
bean_origin,
cocoa_percent,
-- Create a message by concatenating values with "+"
@string1 + ' ' + bean_origin + ' ' + @string2 + ' ' + CAST(cocoa_percent AS nvarchar) AS message1
FROM ratings
WHERE
company = 'Ambrosia'
AND bean_type <> 'Unknown';
Concat Function
To add multiple string together
Concat()
DECLARE @string1 NVARCHAR(100) = 'Chocolate with beans from';
DECLARE @string2 NVARCHAR(100) = 'has a cocoa percentage of';
SELECT
bean_type,
bean_origin,
cocoa_percent,
-- Create a message by concatenating values with "+"
@string1 + ' ' + bean_origin + ' ' + @string2 + ' ' + CAST(cocoa_percent AS nvarchar) AS message1,
-- Create a message by concatenating values with "CONCAT()"
CONCAT(@string1,' ' ,bean_origin , ' ', @string2,' ',cocoa_percent) AS message2
FROM ratings
WHERE
company = 'Ambrosia'
AND bean_type <> 'Unknown';
Contact_WS()
DECLARE @string1 NVARCHAR(100) = 'Chocolate with beans from';
DECLARE @string2 NVARCHAR(100) = 'has a cocoa percentage of';
SELECT
bean_type,
bean_origin,
cocoa_percent,
CONCAT_WS(' ', @string1, bean_origin, @string2, cocoa_percent) AS message3
FROM ratings
WHERE
company = 'Ambrosia'
AND bean_type <> 'Unknown';
STRING_AGG Function Using Group by
SELECT
company,
-- Create a list with all bean origins ordered alplabetically
STRING_AGG(bean_origin, ',') WITHIN GROUP (ORDER BY bean_origin) AS bean_origins
FROM ratings
WHERE company IN ('Bar Au Chocolat', 'Chocolate Con Amor', 'East Van Roasters')
-- Specify the columns used for grouping your data
GROUP BY company;
String SPLIT Function
DECLARE @phrase NVARCHAR(MAX) = 'In the morning I brush my teeth. In the afternoon I take a nap. In the evening I watch TV.'
SELECT value
FROM STRING_SPLIT(@phrase,'.');
You will get the chance to use functions like: LEN()
, UPPER()
, PATINDEX()
, CONCAT()
, REPLACE()
and SUBSTRING()
SELECT
-- Concatenate the first and last name
CONCAT('***' , first_name, ' ', UPPER(last_name), '***') AS name,
-- Mask the last two digits of the year
REPLACE(birthdate, SUBSTRING(CAST(birthdate AS varchar), 3, 2), 'XX') AS birthdate,
email,
country
FROM voters
-- Select only voters with a first name less than 5 characters
WHERE LEN(first_name) < 5
-- Look for this pattern in the email address: "j%[0-9]@yahoo.com"
AND PATINDEX('j_a%@yahoo.com', email) > 0;
Comments
Post a Comment