SQL Mathematical Operators and String Operators

All of them can be directly used in any SELECT or WHERE clause.

Mathematical Operators

OperatorMeaningExampleResult
+additionSELECT 10+212
-subtractionSELECT 10-28
*multipicationSELECT 10*220
/divisionSELECT 10/25
%modulo (remainder)SELECT 10%31
^exponentiation (associates left to right)SELECT 2^38
| /square root| / 25.05
| |/cube root|| / 27.03
!factorial5 !120
SELECT 1050 * (1 + 0.10) AS "price incl GST"

Output:
在这里插入图片描述

Mathematical functions

mod(a, b)
Computes the remainder of a / b.
round(n, d)
Rounds n to d decimal places.
trunc(n, d)
Truncates n to d decimal places.
ceil(n)
Computes the smallest integer value not less than n.
floor(n)
Computes the largest integer value not greater than n.
abs(n)
Computes the absolute value of n.
pi()
“π” constant

SELECT pi();

在这里插入图片描述

SELECT mod(15, 4), round(15.67, 1),
       trunc(15.67, 1), ceil(15.67),
       floor(15.67), abs(-121.4)

Output:
在这里插入图片描述

String Operators

ItemMeaningExampleResult
||String concatenationSELECT 'Hello, ’ || ‘world!’Hello, world!
lower(s)Convert string s to lower caseSELECT lower(‘Hello, world!’)hello, world!
upper(s)Convert string s to upper caseSELECT upper(‘Hello, world!’)HELLO, WORLD!

Example:
write an SQL query to list the actor_id and fullname of every Actor whose last_name begins with ‘A’.

SELECT actor_id, last_name||', '||first_name as fullname
FROM Actor
WHERE last_name LIKE 'A%'

Output:
在这里插入图片描述
Reference:
Mathematical Operators

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值