连接数据
在 SQL Server 中,常常需要把若干部分拼接成一个字符串。您可能需要把来自不同列的信息组合起来,并将结果作为一个整体发送给其他应用。 在本练习中,您将熟悉连接数据的不同方法。
您将创建类似这样的消息:"Chocolate with beans from Belize has a cocoa percentage of 0.6400"。
这句话由两个字符串变量与 ratings 表中 bean_origin 和 cocoa_percent 列的数据拼接而成。
为限制结果集,查询只选择公司名称为 "Ambrosia" 且 bean_type 非 unknown 的记录。
本练习是课程的一部分
在 SQL Server 中使用函数处理数据
交互式实操练习
通过完成这段示例代码来试试这个练习。
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(___ AS nvarchar) AS message1
FROM ratings
WHERE
company = 'Ambrosia'
AND bean_type <> 'Unknown';