缩短长字符串
evanston311 表中的 description 列可能很长。您可以使用 length() 函数获取字符串长度。
为了展示或快速浏览数据,您可能只想显示前几个字符。您可以使用 left() 函数获取每个值开头的指定数量的字符。
为表明还有更多内容,可以在被缩短的 description 末尾连接 '...'。为此,您可以使用 CASE WHEN 语句,仅在字符串长度大于 50 时添加 '...'。
当 description 以"单词" "I" 开头时,选择 description 的前 50 个字符。
本练习是课程的一部分
SQL 中的探索性数据分析
练习说明
当
description的length()大于 50 个字符时,选择description的前 50 个字符,并在末尾连接'...';否则按原样选择description。仅选择以"单词" 'I' 开头而非以字母 'I' 开头的描述。
- 例如,您应当选择 "I like using SQL!",但不应选择 "In this course we use SQL!"。
交互式实操练习
通过完成这段示例代码来试试这个练习。
-- Select the first 50 chars when length is greater than 50
SELECT CASE WHEN length(___) ___ ___
THEN ___(___, ___) || ___
-- otherwise just select description
ELSE description
END
FROM evanston311
-- limit to descriptions that start with the word I
WHERE ___ LIKE ___
ORDER BY description;