SQL SERVER-4-日期时间函数|字符串函数|over函数|over函数|case函数


1.日期时间函数

--获取数据库时间,精确到毫秒
print SYSDATETIME()

--获取数据库时间,精确到分
print getdate()

--返回代表指定日期的指定日期部分的字符串
print datename(year,getdate())
print datename(month,getdate())
print datename(day,getdate())

--返回代表指定日期的指定日期部分的整数
print datepart(year,getdate())
print datepart(month,getdate())
print datepart(day,getdate())
--他们的区别
print datename(year,getdate())+'10'
print datename(year,getdate())+10
print datepart(year,getdate())+'10'
print datepart(year,getdate())+10

--在指定的日期上加一定的时间或日期后的新值
--增加两个月
select dateadd(month,2,getdate())
--增加1000天
select dateadd(day,1000,getdate())
--减少1000天(负数为减去指定的数量)
select dateadd(day,-2000,getdate())


--使用dateadd()函数,查询出入职一年以上的员工
--直接增加一年
select * from Test1002
where dateadd(year,1,JoinDate)<=getdate()
--增加365天
select * from Test1002
where dateadd(day,365,JoinDate)<=getdate()

--DateDiff函数,获取两个日期时间之间的差值。根据单位不同,返回值不同。
select datediff(year,'1990-10-10',getdate())
select datediff(month,'1990-10-10',getdate())
select datediff(day,'1990-10-10',getdate())
select datediff(hh,'1990-10-10',getdate())
select datediff(mi,'1990-10-10',getdate())
select datediff(ss,'1990-10-10',getdate())


--year(),month(),day()函数获取日期中的年月日
select year(getdate())
select month(getdate())
select day(getdate())

--计不同年份出生的同学的人数。
select
    datepart(year,tsBirthday) as 年份,
    count(1) as 出生的人数
from TblStudent
where tsbirthday is not null
group by datepart(year,tsBirthday)

--统计不同年龄的人数
select
    datediff(year,tsbirthday,getdate()) as 年龄,
    count(*) as 出生的人数
from TblStudent
group by datediff(year,tsbirthday,getdate())

--下面是指定应返回的日期部分的参数,识别的日期部分和缩写
--日期部分     缩写
--year          yy, yyyy
--quarter      qq, q
--month      mm, m
--dayofyear  dy, y
--day          dd, d
--week          wk, ww
--weekday      dw
--Hour          hh
--minute      mi, n
--second      ss, s
--millisecond   ms

2.字符串函数
--len计算字符长度
--显示字符的个数,无论中文英文一个字符就是一个字符
select len('我你Ta')

--这个不是字符串函数
--这个是计算字符字节数,汉字为两个字节,字母为一个字节
select datalength('我你Ta')
--加上N之后表示按unicode的方式计算字节长度,在unicode中汉字和英文都是两个字节
select datalength(N'我你Ta')--表示所占用的字节数

--给一个字符返回他的ASCII吗
select ASCII ('长')
select ASCII ('林')
select ASCII ('a')
select ASCII ('A')

--给一个整数,返回对应的字符,范围为0~255,如果不在这个范围内则返回null
select CHAR(65)
select CHAR(196)
select char(111)

--转换为大写
select upper('abcdefg')

--转换为小写
select lower('ASADF')

--去掉字符串左边的空格
print ltrim(' 公交车的轮子转啊转 ')

--去掉字符串右边的空格
print rtrim(' 公交车的轮子转啊转 ')

--从左边开始截取字符
select left('公交车的轮子转啊转',3)

--从右边开始截取字符
select right('公交车的轮子转啊转',3)

--截取字符串,下标从1开始,截取三个长度
select substring('公交车的轮子转啊转',1,3)
--开始下标为2,则截取的字符串包括下标2
select substring('公交车的轮子转啊转',2,3)

3.over函数


--over函数也叫开窗函数

--over函数的写法
--over(partition by class order by sroce)  --按照sroce排序进行累计,order by是个默认的开窗函数,按照class分区

--开窗的窗口范围
--over(order by sroce range between 5 preceding and 5 following)  --窗口范围为当前行数据幅度减5加5后的范围内的。
--over(order by sroce rows between 5 preceding and 5 following)   --窗口范围为当前行前后各移动5行


--over子句演示
select sex,count(*) from ZY_BRSYK group by sex

--这样写是错误的,应为没有吧syxh放到聚合函数中
select syxh,sex,count(*) from ZY_BRSYK group by sex

--可以使用over()实现,按照sex分区
select
    syxh,
    sex,
    count(*) over(partition by sex)
from ZY_BRSYK


select
    syxh,
    sex,
    count(*) over()  --over()中什么都不写,表示把整个表分成了一个区,统计出来的是记录的总条数
from ZY_BRSYK

4.case函数

case expr
    when 值 then 值
    when .. then ..
end

case
    when 条件 then 值
    when 条件 then 值
    else 值
end
--表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。(见备注1)

select
    列1=case
            when A>B then A
            else B
        end,
    列2=case
            when B>C then B
            else C
        end
from T8


--在订单表中,统计每个销售员的总销售金额,
--列出销售员名、总销售金额、称号(>6000金牌,>5500银牌,>4500铜牌,否则普通)
select * from MyOrders
select
    销售员,
    销售总金额=sum(销售数量*销售价格),
    称号=case
            when sum(销售数量*销售价格)>6000 then '销售冠军'
            when sum(销售数量*销售价格)>5500 then '销售亚军'
            when sum(销售数量*销售价格)>4500 then '销售季军'
            else '自己玩去吧'
         end
from MyOrders
group by 销售员


--在成绩表中,查询的时候增加一列,显示“优”、“良”、“中”、“差”
--英语或数学只要有一门功课没有高于70分→ “差”
--两门课都高于70分
--都高于85分
--都高于95分算是优

select * from TblScore
select
    *,
    等级=
        case
            when tEnglish>=95 and tMath>=95 then '优'
            when tEnglish>=85 and tMath>=85 then '良'
            when tEnglish>=70 and tMath>=70 then '中'
            else '差'
        end
from TblScore

--把收入字段拆分成收入和支出两列
select * from test
select
    number,
    收入=
        case
            when amount>0 then amount
            else 0
        end,
    支出=
        case
            when amount<0 then abs(amount)
            else 0
        end
from test

--把胜负拆封成胜 , 负两列
select * from  TeamScore
select
    teamName,
    胜=
        sum(case
                when gameresult='胜' then 1
                else 0
            end),
    负=sum(case when gameresult='负' then 1 else 0 end)
from TeamScore
group by teamName


select
    *,
    teamName,
    胜=case when gameresult='胜' then 1 else 0 end,
    负=case when gameresult='负' then 1 else 0 end
from TeamScore
--
select * from nbascore

select
    teamName,
    第一赛季=max(case when seasonname='第1赛季' then score else null  end),
    第二赛季=max(case when seasonname='第2赛季' then score else null  end),
    第三赛季=max(case when seasonname='第3赛季' then score else null  end)
from nbascore
group by teamname

猜你喜欢

转载自blog.csdn.net/m0_37532448/article/details/82867820