大家帮我写个SQL语句

来源:百度知道 编辑:UC知道 时间:2024/05/21 17:27:46
要求:搜索所有的距当前时间不超过3个月的所有用户信息,如果存在返回true,否则返回FALSE
表中有个上次登录的时间CHG_DT,距当前系统时间不超过三个月。

sql:
select * from 表名 where datediff(Month,CHG_DT,getdate()) <=3
SQL中使用了datediff()函数

你把时间说明确点,是什么时间距当前时间不超过3个月,要不条件怎么写啊,你可以用sysdate取当前时间

declare @num int
select @num= count(CHG_DT) from 表名where (month(CHG_DT)-month(getDate()))<=3
if @num>0 print 'true'
if @num=0 print 'flase'

这个我测试过了,是可以的
这个是在针对服务器上的时间(也只能以服务器时间)来判定的,不是以你所操作的电脑的时候为准(如果你所执行这条语句的电脑不是服务器的话)

楼上的,不知道不要乱说
month()函数你取到的值可能是其他类型的吗?

楼上的思路不错,不过得补充下:

declare @num int
select @num= count(CHG_DT) from 表名 where cast ((month(CHG_DT)-month(getDate())) as int) <=3
if @num>0 print 'true'
if @num=0 print 'flase'

先转换下,然后再比较!

不然不是同类型的不能够做比较

望好好学习,天天向上!