Sams Teach Yourself SQL in 10 Minutes(Fourth Edition)学习记录

第八课 本课学习什么是函数,什么类型的函数DBMS支持及怎样使用这些函数。

8.1 理解函数(Understanding Functions)

8.1.1 函数的问题

使用SQL函数可能非常不确定。SQL函数倾向于与特定DBMS相关。

Sams Teach Yourself SQL in 10 Minutes(Fourth Edition)学习记录_第1张图片
image.png
SQL函数不可移植。
许多SQL编程人员选择不使用任何实现特定的特性。
Tip:Should You Use Functions?

So now you are trying to decide whether you should or shouldn’t use functions. Well, that decision is yours, and there is no right or wrong choice. If you do decide to use functions, make sure you comment your code well, so that at a later date you (or another developer) will know exactly what SQL implementation you were writing to.

8.2 使用函数(Using Functions)

1、文本函数用于操作文本字符串(裁剪或者填补值及将值做大小写转换)。
2、数值函数用于对数值数据做做数学运算操作(返回绝对值和做代数计算)。
3、日期和时间函数用于操作日期和时间值及从这些值中提取特定组件(返回日期间的差异及检查日期有效性)。
4、系统函数返回跟使用的DBMS有关的特殊信息(如返回用户登录信息)。

8.2.1 文本操作函数

RTRIM()
UPPER()

输入:

SELECT vend_name, UPPER(vend_name) AS vend_name_upcase
FROM Vendors
ORDER BY vend_name;
Table 8.2. Commonly Used Text-Manipulation Functions
Sams Teach Yourself SQL in 10 Minutes(Fourth Edition)学习记录_第2张图片
image.png
Note: SOUNDEX Support

SOUNDEX() is not supported by Microsoft Access or PostgreSQL, and so the following example will not work on those DBMSs. In addition, it is only available in SQLite if the SQLITE_SOUNDEX compile-time option is used when SQLite is built, and as this is not the default compile option, most SQLite implementations won’t support SOUNDEX().

输入:

SELECT cust_name, cust_contact
FROM Customers
WHERE cust_contact = 'Michael Green';

输入:

SELECT cust_name, cust_contact
FROM Customers
WHERE SOUNDEX(cust_contact) = SOUNDEX('Michael Green');
8.2.2 日期和时间操作函数(Date and Time Manipulation Functions)

输入:

SELECT order_num
FROM Orders
WHERE DATEPART(yy, order_date) = 2012;

输入:

SELECT order_num
FROM Orders
WHERE DATEPART('yyyy', order_date) = 2012;

输入:(PostgreSQL)

SELECT order_num
FROM Orders
WHERE DATE_PART('year', order_date) = 2012;

输入:(oracle)

SELECT order_num
FROM Orders
WHERE to_number(to_char(order_date, 'YYYY')) = 2012;

输入:(MySQL and MariaDB)

SELECT order_num
FROM Orders
WHERE YEAR(order_date) = 2012;

输入:(SQLite)

SELECT order_num
FROM Orders
WHERE strftime('%Y', order_date) = 2012;

8.2.3 数字操作函数(Numeric Manipulation Functions)

代数的、三角的、几何的运算

Table 8.3. Commonly Used Numeric Manipulation Functions


Sams Teach Yourself SQL in 10 Minutes(Fourth Edition)学习记录_第3张图片
image.png

8.3 总结

In this lesson, you learned how to use SQL’s data manipulation functions. You also learned that although these functions can be extremely useful in formatting, manipulating, and filtering data, the function details are very inconsistent from one SQL implementation to the next (as demonstrated by the differences between SQL Server and Oracle).

你可能感兴趣的:(Sams Teach Yourself SQL in 10 Minutes(Fourth Edition)学习记录)