• 数学函数,字符串函数


    --数学函数
    --ABS绝对值
    select ABS(-7)
    --ceiling取上线,
    select CEILING(3.14)
    --floor去下线
    select floor(3.14)
    --power几次方,
    select POWER (2,3)
    --round 四舍五入,
    select ROUND(3.5,0)
    --sqrt 开平方根
    select SQRT(4)
    --square 平方
    select SQUARE(4)
    --ascii 返回字符串最左边的字符的ascii码
    select ASCII('name')
    --select ASCII name from xueshengxinxi--查看所有人名的首字符的ascii码
    --char 将ascii码转换成字符
    --LEN 返回字符串长度
    select LEN('abcdefg')as 长度
    --charindex 返回字符串首个字符出现在某个字符串从头开始位几的索引
    select CHARINDEX('d','abcdefgh')--索引从一开始,若返回值为零,表示没有找到
    --difference 返回相似度,用0-4表示相似程度
    --select DIFFERENCE('abcdefgh','dch')
    --left 表示从左边截取字符串
    select LEFT('abckKkklvhjvj',4)
    --right 表示从右边截取字符串
    select right('abckKkklvhjvj',4)
    --lower 全部转换成小写
    select LOWER('SAkfKkjabk')
    --upper 全部转换成大写
    --ltrim 去掉左边的空格
    select LTRIM(' abc ')
    --rtrim 去掉右边的空格
    select RTRIM(' abc ')
    --PATINDEX 相当于charindex
    --replace 查找并替换
    --select REPLACE(sex,'女','姑娘')from student
    --replicate 复制粘贴
    select REPLICATE('abc',2)
    --reverse 翻转
    select REVERSE('sggzabzsb')
    --space 空格
    select 'a'+SPACE(5)+'abc'
    --str 强制转换成字符串
    --参数一是需要转换的字符,二是转换之后保留的长度,三是小数点后保留的位数
    --注意:参数二在小于参数一的整数部分位数时无法转换
    select STR(5478.6331,7,3)
    --stuff 从第几个索引的位置,看看需不需要向后删除几位
    --然后将需要插入的内容插入
    --参数一是需要被插入的字符串
    --参数二是从第几个索引开始
    --参数三是是否需要向后删除几个字符
    --参数四是新插入的字符
    select STUFF('abcdef',3,0,'hsgh')
    --substring 截取字符串
    select SUBSTRING('ksfksdv',2,3)

    练习:

    create table lianxi
    (
    code int,
    name varchar (20),
    sex char(10),
    tizhong decimal(18,1),
    age int,
    yuwen decimal (18,1),
    shuxue decimal (18,1),
    yiyu decimal (18,1),
    banji varchar(20)

    )
    go
    insert into xueshengxinxi values(1.,'赵一','男',55,18,78,82,90,'一班')
    insert into xueshengxinxi values(2.,'钱二','女',50,19,75,92,80,'二班')
    insert into xueshengxinxi values(3.,'孙三','男',68,20,58,78,94,'一班')
    insert into xueshengxinxi values(4.,'李四','女',58,24,68,89,76,'二班')
    insert into xueshengxinxi values(5.,'周五','男',75,23,75,74,56,'二班')
    insert into xueshengxinxi values(6.,'郑六','女',54,21,78,80,84,'一班')
    insert into xueshengxinxi values(7.,'武七','男',64,21,76,72,84,'二班')
    insert into xueshengxinxi values(8.,'王八','女',61,17,74,62,47,'一班')
    insert into xueshengxinxi values(9,'杜九','女',57,19,76,85,94,'二班')
    insert into xueshengxinxi values(10.,'李晨','男',78,20,75,84,90,'一班')
    insert into xueshengxinxi values(11.,'王宝强','男',75,19,78,42,90,'二班')
    insert into xueshengxinxi values(12.,'陈颖','女',51,20,75,82,60,'一班')
    insert into xueshengxinxi values(13.,'张曼玉','女',49,19,68,82,70,'二班')
    insert into xueshengxinxi values(14.,'李连杰','男',75,21,88,92,70,'一班')
    insert into xueshengxinxi values(15.,'胡歌','男',67,20,90,81,98,'一班')
    insert into xueshengxinxi values(16.,'霍建华','男',68,19,78,85,60,'二班')
    insert into xueshengxinxi values(17.,'王凯','男',69,20,75,92,50,'一班')
    insert into xueshengxinxi values(18.,'刘涛','女',52,18,79,72,59,'一班')
    insert into xueshengxinxi values(19.,'刘老根','男',70,20,56,81,64,'二班')
    insert into xueshengxinxi values(20.,'林青霞','女',60,18,85,67,91,'一班')
    go
    --查询所有信息
    select *from lianxi
    --查询所有姓王的人的信息
    select * from xueshengxinxi where name like'王%'
    --查询一班所有学生信息
    select banji as 班级, * from lianxi where banji='一班'group by banji
    --查询二班所有人员的信息
    select banji as 班级, * from lianxi where banji='二班'group by banji

    --查看所有女同学并且体重过65的姓名
    select name from lianxi where sex='女' and tizhong>65

    --(分开查询)
    --查一班语文最高分、最低分学生的所有信息
    select top 1 banji as 班级 from lianxi where banji='一班'group by banji order by yuwen
    select top 1 banji as 班级 from lianxi where banji='一班'group by banji order by yuwen desc

    --查一班数学最高分、最低分学生的所有信息
    select top 1 banji as 班级 from lianxi where banji='一班'group by banji order by shuxue
    select top 1 banji as 班级 from lianxi where banji='一班'group by banji order by yuwen desc

    --查一班英语最高分、最低分学生的所有信息

    --一班所有人员信息按照语文降序排列
    select banji as 班级 ,*from lianxi where banji ='一班' group by banji order by yuwen desc
    --二班所有人员信息按照英语升序排列
    --两个班英语过75分的人数
    select banji as 班级,COUNT (*) from lianxi where yiyu>75 group by banji
    --数学过70分并且人数超过3个的班级
    select banji as 班级,COUNT (*) from lianxi where shuxue>70 group by banji having COUNT (*)>3

    --两个班体重超65的人数
    select banji as 班级 ,COUNT (*)from lianxi where tizhong>65 group by banji
    --体重超过65的并且人数超过3个的班级
    select banji as 班级 ,COUNT (*)from lianxi where tizhong>65 group by banji having COUNT(*)>3

  • 相关阅读:
    BZOJ 1707 [Usaco2007 Nov]tanning分配防晒霜
    BZOJ2697:特技飞行
    Bridging signals(NlogN最长上升子序列)
    HDU 4291 A Short problem(矩阵+循环节)
    计蒜之道 初赛第一场B 阿里天池的新任务(简单)
    1008: [HNOI2008]越狱
    1022: [SHOI2008]小约翰的游戏John
    1192: [HNOI2006]鬼谷子的钱袋
    2456: mode
    BZOJ-1968
  • 原文地址:https://www.cnblogs.com/dulovexin/p/4978104.html
Copyright © 2020-2023  润新知