当前位置:网站首页>重要的 SQL Server 函数 - 其他函数
重要的 SQL Server 函数 - 其他函数
2022-07-28 04:16:00 【Navicat中国】
第 4 部分:其他函数
最后一类重要的 SQL Server 函数包括那些处理 NULL、转换和控制流的函数。这些函数绝不是剩菜,是你可能会遇到的最有用的函数之一!
COALESCE
每当你选择值不是强制性的列时,你肯定会遇到 NULL 值。这是有道理的,因为 NULL 值表示没有值或缺少的信息。麻烦的是,如果将 NULL 值包括在计算以及可能对列数据执行的其他操作中,则可能会造成严重破坏。
COALESCE 函数接受一个参数列表,并返回不包含空值的第一个参数。 因此,SQL Server 会遍历你提供的每个输入参数,直到遇到一个不为 NULL 的参数或者只是遍历了所有参数。这是它的语法:
COALESCE(val1, val2, ...., val_n)
用零代替 NULL 值是很平常的事。在某些情况下,不同的值可能更有意义。例如,Sakila 示例数据库的 film 表中有一个名为 original_language_id 的列,用于非英语的电影。每当找到 NULL 值时,我们可以使用 COALESCE 将其值设置为 1(英语的 language_id):

CONVERT
将输出值转换为指定的数据类型是在意料之中的数据库工作。在 SQL Server 中,可以使用 CONVERT 函数将值的数据类型更改为另一种。它的语法很简单:
CONVERT(type, value)
使用 CONVERT 的一个很好的理由是从 datetime 字段中删除时间部分。以下的查询以原始日期时间格式显示相同的字段,但没有时间部分:

IIF
IF/ELSE 语句是编程中最常用的控制流结构。SQL Server 以 IIF 函数的形式为我们的查询提供 IF/ELSE 语句的功能。它的语法是:
IIF(expression, value_if_true, value_if_false)
我们可以利用 IIF 函数将电影长度分为三类:Short(短)、Medium(中)和 Long(长),取决于它们的长度。我们将电影长度分类如下:
- Short:80 分钟以下
- Medium:80 至 120分钟之间
- Long:超过 120 分钟
我们使用 IIF() 函数将电影的长度与给定的表达式进行比较,并根据结果返回 1 或 NULL。如果返回 1,则将计入该列标题(Short、Medium 或 Long)下:

总结
这是有关最重要的 SQL Server 函数的系列文章的结尾。如本系列开头所述,了解特定数据库类型的确切函数名和签名会有所帮助,因为它们在不同供应商之间可能会有所不同。例如,IIF 函数仅在 Microsoft 产品中可以找到。如果你对 Navicat for SQL Server感兴趣,可以免费试用 14 天!
往期回顾
边栏推荐
- Is there a bonus period for robot engineering
- 一名合格的软件测试工程师,应该具备哪些技术能力?
- 7/27 (board) dyeing method to determine bipartite graph + find combination number (recursive formula)
- Thoroughly understand the sharing function in wechat games
- MATLB | location and constant volume IEEE30 node implementation of distributed energy
- Null security and exception
- Linux - MySQL advanced (day19)
- Information system project manager (2022) - key content: Project Procurement Management (12)
- Shell rental reptile
- There are so many ways to view the web source code! Do you know?
猜你喜欢

Fearless of side impact damage, Chery arize 8 fully protects the safety of passengers

Regression - linear regression
![[kinematics] simulation of orbital angular momentum based on MATLAB [including Matlab source code 1971]](/img/5e/dfe029490183ee74687606941ce98e.jpg)
[kinematics] simulation of orbital angular momentum based on MATLAB [including Matlab source code 1971]

功耗:Leakage Power

Thoroughly understand the sharing function in wechat games

RT-Thread改变打印串口(在BSP的基础上添加其他功能)

glusterfs 文件未挂载,权限: r-s

Information system project manager (2022) - key content: Project Procurement Management (12)

What is interface testing and its testing process

Do Netease and Baidu have their own tricks for seizing the beach AI learning machine?
随机推荐
Null security and exception
When import is introduced, sometimes there are braces, sometimes there are no braces. How should we understand this?
Esp8266 WiFi module and mobile communication
功耗:Leakage Power
Information system project manager (2022) - key content: Knowledge Management (15)
【无标题】
Kingbasees Security Guide for Jincang database -- 6.1 introduction to strong authentication
Construction and use of FTP server and NFS server
H265/HEVC名词解释-- CTU,CTB,CU,CB,TU,PU,TB,PB,LCU,Slice,Tile,Chroma,Luma,I帧,B帧,P帧
Detailed explanation of pl/sql parameters ("box model")
Skillfully use stack backtracking to help you quickly locate problems
Slice切片
21天,胖哥亲自带你玩转OAuth2
2022-7-27 顾宇佳 学习笔记
servlet使用
"Three no's and five requirements" principle of enterprise Digitalization Construction
Filters, interceptors, listeners
Detailed explanation of string + memory function (C language)
Information system project manager (2022) - key content: Strategic Management (17)
Information system project manager (2022) - key content: Project Procurement Management (12)