SQl基础教程——费希利(第六章 函数,谓词,CASE表达式)

各种各样的函数

函数函数

算数函数

字符串函数

日期函数

转换函数

谓词

like函数——字符串部分的一直查询

BETWEEN——范围查询

is null,is not null——判断是否为null

使用子查询作为In谓词的参数

EXIST谓词。

6-1各种各样的函数

函数:输入某一个值得到得到相应的结果,输入的值叫参数,输出的值叫返回值。

算数函数是最基本的函数,如+ - * /

创建表:

create table SampleMath

(

m numeric(10,3),

n int,

p int

);

插入数据

insert into SampleMath(m,n,p) values(500,0,null);

insert into SampleMath(m,n,p) values(-180,0,null);

insert into SampleMath(m,n,p) values(null,null,null);

insert into SampleMath(m,n,p) values(null,7,3);

insert into SampleMath(m,n,p) values(null,5,2);

insert into SampleMath(m,n,p) values(null,4,null);

insert into SampleMath(m,n,p) values(8,null,3);

insert into SampleMath(m,n,p) values(2.27,1,null);

insert into SampleMath(m,n,p) values(5.5555,2,null);

insert into SampleMath(m,n,p) values(null,1,null);

insert into SampleMath(m,n,p) values(8.76,null,null);


查询表

ABS绝对值

ABS是计算绝对值的函数。绝对值不考虑数值的符号,表示一个数到圆点的距离。绝对值的计算方法就是:0和正数的绝对值是其本身,负数的绝对值是去掉负号之后的结果。


运行之后的结果

MOD求余

MOD是计算除法余数的函数。但是sql server不支持函数。

sql server使用特殊符号%计算余数。

select n,p,n%p as '余数' from SampleMath

ROUND——四舍五入

ROUND函数进行四舍五入操作。四舍五入英文称为round。


四舍五入

字符串函数

create table SampleMaths

(

str1 varchar(40),

str2 varchar(40),

str3 varchar(40),

)

insert into SampleMaths(str1,str2,str3)values('opx','rt',NULL)

insert into SampleMaths(str1,str2,str3)values('abc','def',NULL)

insert into SampleMaths(str1,str2,str3)values('山田','太浪','是我')

insert into SampleMaths(str1,str2,str3)values('aaa',null,null)

insert into SampleMaths(str1,str2,str3)values(null,'xyz',null)

INSERT INTO SampleMaths (str1, str2, str3) VALUES ('@!#$%' ,

NULL ,NULL);

INSERT INTO SampleMaths (str1, str2, str3) VALUES ('ABC' ,

NULL ,NULL);

INSERT INTO SampleMaths (str1, str2, str3) VALUES ('aBC' ,

NULL ,NULL);

INSERT INTO SampleMaths (str1, str2, str3) VALUES ('abc太郎' ,

'abc' ,'ABC');

INSERT INTO SampleMaths (str1, str2, str3) VALUES ('abcdefabc' ,

'abc' ,'ABC');

INSERT INTO SampleMaths (str1, str2, str3) VALUES ('micmic' ,

'i' ,'I');


表中的数据

||   拼接函数.在实际业务中,我们经常会碰到 abc + de = abcde 这样希望将字符串

进行拼接的情况。

sql server无此用法,而是使用+号连接字符串。

SELECT str1, str2, str1+ str2 as '拼接' FROM SampleMaths;


拼接

LENGTH——字符串长度

可以判断出字符串的长度。


SQL中的长度的用法

sql中有很多特定的用法。

LOWER——小写转化

select str1,LOWER(str1) from SampleMaths

LOWER(str1)返回值

Upper大写转化


大写转化

SUBSTRING——字符串截取

select str1,SUBSTRING(str1,3,2) as '截取' from SampleMaths

截取

case表达式

什么是case表示?

case表达式

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容