SQL server函數(shù)查詢

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','趙強(qiáng)','1992-02-01',null,null);

1.查詢學(xué)生表中最長(zhǎng)姓名的長(zhǎng)度。

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

2.查詢學(xué)生表中姓名最長(zhǎng)的學(xué)生信息。

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

3.修改學(xué)生信息,將學(xué)號(hào)中的2009替換成09。

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

4.查詢姓名為“王小虎”的學(xué)號(hào)的最末兩位。

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

5.計(jì)算學(xué)生的年齡,替換學(xué)生表中的age列。

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

6.查詢出生年是1991的學(xué)生信息。

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

7.查詢出生月份是1至6月的學(xué)生信息。

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

8.查詢當(dāng)月有哪些學(xué)生過生日。

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

9.計(jì)算下一次過生日的日期,替換學(xué)生表中的nextbirth列。

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

10.計(jì)算下一次過生日距離現(xiàn)在還有多少天。

select datediff(
day,
getdate(),
dateadd(year,ceiling(datediff(day,birthday,getdate())/365.0),
birthday)
) from student
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、創(chuàng)建表 創(chuàng)建表Student: 創(chuàng)建表Course: 創(chuàng)建表SC: 二、修改表基本表: alter table...
    流水潺湲閱讀 1,762評(píng)論 0 0
  • SQL語言基礎(chǔ) 本章,我們將會(huì)重點(diǎn)探討SQL語言基礎(chǔ),學(xué)習(xí)用SQL進(jìn)行數(shù)據(jù)庫(kù)的基本數(shù)據(jù)查詢操作。另外請(qǐng)注意本章的S...
    厲鉚兄閱讀 5,456評(píng)論 2 46
  • T-SQL語句創(chuàng)建表 如圖1. IDENTITY的應(yīng)用 在圖1創(chuàng)建的兩個(gè)表中,SortId和CommodityId...
    肉肉要次肉閱讀 988評(píng)論 0 0
  • 今天是本輪打卡的第21天,也是最后一天。簡(jiǎn)單回顧了這一年,除了中間有兩個(gè)月退出打卡組織后,其余時(shí)間都是打卡貫穿了每...
    W南茜閱讀 820評(píng)論 0 0
  • 道理每個(gè)人都懂,勸慰別人,大多數(shù)人信手拈來頭頭是道,然而,事情一旦發(fā)生在自己身上,就開始惴惴不安,惶惶不可終日,不...
    南情北續(xù)閱讀 333評(píng)論 0 0

友情鏈接更多精彩內(nèi)容