SQL函数

  知识点--函数:封装功能的
     自带函数
     1.字符串函数
     
     2.时间函数
     
     3.数字函数
     
     4.其他函数
--datetime
create table a(
   tid int ,
   ttime datetime
)
insert into a values(1,'2020-2-2 14:22')
select * from a;

--字符函数
--charindex()在字符串中查询此字符串的位置
select charindex('b','accbdbe')
--len()字符串长度
select len('abcd哈')
--lower()小写/upper()大写
select lower('abCD')
select upper('abCD')
--ltrim()/rtrim()  去除左右空格
select ltrim('  abcd')
select rtrim( 'abcd  ')
--left()/right()左右截取几位数
select left('abcd',2)
select right('abcd',2)
--replace()替换
select replace('abcde','c','x')

--日期函数
--getdate()获取当前时间
select getdate()
--year()/month()/day()年月日
--年yyyy 月mm 日dd 时hh 分mi 秒ss
select year('2020-1-1')
select month('2020-1-1')
select day('2020-1-18')
--datename()根据表达式获取年月日
select datename(YYYY,'2019-10-1')
select datename(mm,'2019-10-1')
select datename(dd,'2019-10-1')
select datename(hh,'2019-10-1 14:22')
--dateadd()增加年月日
select dateadd(mm,14,'2020-1-1')
--datediff()时间差
select datediff(mm,'2020-10-1','2021-1-1')

--数学函数
--abs()绝对值
select abs(-22)
--ceiling()向上取整 floor()向下取整
select ceiling(2.1111)
select floor(2.92222)
--round()四舍五入
select round(2.44,1)
--平方根 sqrt()
select sqrt(13)
--判断正数负数和0  1 -1 0
select sign(0)


--其他函数
--datalength()字节数
select datalength('abcd哈')
--转换函数
--convert()/cast()转换为某某
select convert(varchar(6),123)+'a'
select cast(123 as varchar(5))+'a'
--isnull()把空值转为某某
select isnull(null,'哈哈')

create table tb_zz(
  tid int,
  tage int,
  tmoney int
)
insert into tb_zz values(1,18,20)
insert into tb_zz values(2,28,null)
insert into tb_zz values(1,38,33)

select * from tb_zz;

//insert into tb_zz values(1,18,20)第二位值加第三位值
select tage+isnull(tmoney,0) from tb_zz;

--默认值
create table abc(
   tage int default(18),
   tname varchar(50) 
)
select * from abc;
insert into abc(tname) values('zs')
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欣宇不会敲代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值