菜鸟有个简单的问题

jikuiyu 2008-10-23 08:50:37
birthdate列 的数据类型是 datetime的
现在要查询20到40岁 和 40岁以上的人的资料
哪位老大举个例子 给小弟讲解下
不胜感激
...全文
87 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChinaJiaBing 2008-10-23
  • 打赏
  • 举报
回复

select * from 表 where datediff(yy,birthday,getdate()) >20
ChinaJiaBing 2008-10-23
  • 打赏
  • 举报
回复

select * from 表 where datediff(yy,birthday,getdate()) between 20 and 40
union all
select * from 表 where datediff(yy,birthday,getdate()) >40

-晴天 2008-10-23
  • 打赏
  • 举报
回复
函数 getdate() 可以获得当前时间.
函数 datediff(yy,第一个时间,第二个时间) 可以获得两个时间相差多少年.
-狙击手- 2008-10-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 jikuiyu 的帖子:]
birthdate列 的数据类型是 datetime的
现在要查询20到40岁 和 40岁以上的人的资料
哪位老大举个例子 给小弟讲解下
不胜感激
[/Quote]


select *
from ta
where datediff(yy,birthdate,getdate()) between 20 and 40 --和当前时间相关20年到40年
jikuiyu 2008-10-23
  • 打赏
  • 举报
回复
能不能再给讲的清楚点,我是个初学者,这些专业术语还是不太懂,
谢谢,摆脱了
rucypli 2008-10-23
  • 打赏
  • 举报
回复
datediff(yy,birthdate,getdate())
返回跨两个指定日期的日期和时间边界数
wzy_love_sly 2008-10-23
  • 打赏
  • 举报
回复
create table tb(name varchar(50),birthday datetime)
insert into tb select '张','1982-08-10'
insert into tb select '李','1983-08-10'
go

select *,datediff(yy,birthday,getdate()) as 年龄
from tb
where datediff(yy,birthday,getdate()) between 20 and 30

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧