select * from paxa_user_info where datediff(d,getdate(),convert(datetime,convert(char(6),birthday,110)+cast(year(getdate()) as char))) between 0 and 10
上面的日期都不对吧,SQL Server和Access是有很大区别的,我早就听别人讲过Sql Server只支持Datediff和Datepart两个函数,其SQL语句是不支持date(),month(),now()等vbscript函数的。哈哈,想不到微软自己的数据库都不支持自己的脚本,这样的兼容性真的令人笑掉大牙。
结果我一试,还真是的,我遇到的问题是要查找当天的记录,我曾经试过:
select * from do_user where datevalue(now())=datevalue(数据库字段)
但是根本就不成功,结果提示sql不识别now()函数,结果我把数据库字段改成文本字段,再试
select * from do_user where cstr(datevalue(now())=数据库字段
结果又提示转换时间有错误。
我还试了许多方法,但无一成功。
我真的很气馁了,真拿微软没办法。兼容性呀,兼容性呀!!!
我上面写的不对,日期连不起来,它把它们当成数字相加了。我看下面写的应该没问题了,有点投机取巧了。
SELECT *,replace(getdate(),year(getdate()),year(birthday)), DATEDIFF(d, replace(getdate(),year(getdate()),year(birthday)),birthday)
FROM employee_Txl where DATEDIFF(d, replace(getdate(),year(getdate()),year(birthday)),birthday)<30 and DATEDIFF(d, replace(getdate(),year(getdate()),year(birthday)),birthday)>=0
sql3 = "select * from do_user where DATEDIFF('d',year(now())&'/'&month(now())&'/'&day(now()),year(now())&'/'&month(birthday)&'/'&day(birthday)) >= 0 and DATEDIFF('d',year(now())&'/'&month(now())&'/'&day(now()),year(now())&'/'&month(birthday)&'/'&day(birthday)) <= 10"