运算符-sql

sql 运算符

算术运算符: +,1,* ,/, %

赋值运算符: = 

逻辑运算符: and, or , not

比较运算符: >, <, <=, >=, =, !=(非SQL92标准)

连接运算符:“+”

运算符的优先级

()>算术运算符 > 比较运算符 > 逻辑运算符 > 连接运算符 > 大于赋值运算符

例子

--算术运算符
select 3 + 4 as 加的结果 --求和
go

select 5/2 as 除的结果 --除法(左右两边都是整数,结果就是整数)
go

select 5.0/2 as 除的结果 --左右两边有一个是非整数,结果为非整数
go

select 5%2 as 模 --5里面有2个2,还多的一个就叫模或者余数
go

--赋值运算符, 将等号右边的值符给左边的变量或者表达式
declare @age int
set @age=18
select @age
go

--比较运算符>, < , <=, >=, <>, != (非92标准)
declare @x int, @y int
set @x=8
set @y=10

if @x >=@y
select '@x的值大于等于@y的值'
else
select '@y的值大'

go

--逻辑运算符 and, or, not
--and 左右结果均为true(真)的时候结果为真
--or 左右结果又一个为true(真)的时候结果为真
--not 取反
--优先级
--not > and > or

--例子
-- 运算步骤 (1) not 6>4 false (2) 6 > 3 true (1) and (2) false (3) 3>5 false
if(3>5 or 6>3 and not 6>4)
select 'TRUE(真)'
else
select 'FALSE(假)'
go --结果为FALSE(假)
--连接运算符 +
-- + 左右,都是数值型 + 就是算术运算符
-- + 左右,都是字符型 + 就是连接运算符
-- + 左右,类型不一致, 就需要类型转换
declare @color varchar(4)
set @color='白色'
select '我最喜欢的颜色' + @color
go -- 结果为我最喜欢的颜色白色

猜你喜欢

转载自www.cnblogs.com/zhangxudong-cnblogs/p/10822066.html