SQL server函数查询

create table student(
stuid varchar(20),
stuname varchar(20),
birthday datetime,
age int,
nextbirth datetime
);

insert into student values('200911001','david','1990-08-02',null,null);
insert into student values('200911002','田中一郎','1991-05-25',null,null);
insert into student values('201011001','王小虎','1991-01-18',null,null);
insert into student values('201011002','赵强','1992-02-01',null,null);

1.查询学生表中最长姓名的长度。

 select len(stuname) from student where len(stuname)>=all(select  len(stuname) from student)

2.查询学生表中姓名最长的学生信息。

 select * from student where len(stuname)>=all(select  len(stuname) from student)

3.修改学生信息,将学号中的2009替换成09。

update student
set stuid=replace(stuid,'2009','09')

4.查询姓名为“王小虎”的学号的最末两位。

select right(stuid,2) from student where stuname='王小虎'

5.计算学生的年龄,替换学生表中的age列。

update student
set age=year(getdate())-year(birthday)

6.查询出生年是1991的学生信息。

 select *from student where year(birthday)='1991'

7.查询出生月份是1至6月的学生信息。

select *from student where month(birthday) between 1 and 6

8.查询当月有哪些学生过生日。

select * from student where month(getdate())>= month(birthday)and day(getdate())>=day(birthday)

9.计算下一次过生日的日期,替换学生表中的nextbirth列。

update student
set nextbirth= dateadd(year,ceiling(datediff(day,birthday,getdate())/365.0),birthday)

10.计算下一次过生日距离现在还有多少天。

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

推荐阅读更多精彩内容

  • 一、创建表 创建表Student: 创建表Course: 创建表SC: 二、修改表基本表: alter table...
    流水潺湲阅读 5,560评论 0 0
  • SQL语言基础 本章,我们将会重点探讨SQL语言基础,学习用SQL进行数据库的基本数据查询操作。另外请注意本章的S...
    厲铆兄阅读 10,706评论 2 46
  • T-SQL语句创建表 如图1. IDENTITY的应用 在图1创建的两个表中,SortId和CommodityId...
    肉肉要次肉阅读 4,367评论 0 0
  • 今天是本轮打卡的第21天,也是最后一天。简单回顾了这一年,除了中间有两个月退出打卡组织后,其余时间都是打卡贯穿了每...
    W南茜阅读 4,039评论 0 0
  • 道理每个人都懂,劝慰别人,大多数人信手拈来头头是道,然而,事情一旦发生在自己身上,就开始惴惴不安,惶惶不可终日,不...
    南情北续阅读 2,206评论 0 0