关于用sql语句判断数据库里今天是否有会员生日的问题

时间:2021-03-09 01:05:00
小弟刚接触asp和SQL不久,现在在写一个程序,要求实现通过会员的身份证号来判断今天是否有会员生日的功能。小弟用asp写了一个函数,用来判断和累计今天生日的会员,程序如下:
<%
Function CheckBthDay()
dim rs,sql,tDay,Birthday,BodySum
BodySum=0 '检查今天是否有会员生日
if len(month(now())) = 1 then '月
tDay=tDay & "0" & cstr(month(now()))
else
tDay=tDay & cstr(month(now()))
end if

if len(day(now()))=1 then '日
tDay=tDay & "0" & cstr(day(now()))
else
tDay=tDay & cstr(day(now()))
end if
'response.write tDay

set rs=server.createobject("adodb.recordset")
sql="select M_Name,M_ZhengJian,M_ZJCode from G_Member where M_ZhengJian = '身份证' and Len(M_ZJCode) = 15 or Len(M_ZJCode) = 18 "
rs.open sql,dsn,1,1

do while not rs.eof and not rs.bof
if len(rs("M_ZJCode"))=15 then
Birthday=Mid(rs("M_ZJCode"),9,4)
elseif len(rs("M_ZJCode"))=18 then
Birthday=Mid(rs("M_ZJCode"),11,4)
else
end if
if Birthday=tDay then '如果今天是此会员的生日
BodySum = BodySum + 1
end if
'response.write rs("M_Name") & " " & Birthday & "<br>"
rs.movenext
loop
rs.close

set rs=nothing
set sql=nothing
CheckBthDay = BodySum
End Function
%>
我知道这是最笨和最耗时间的方法。在此请教大家。有没有可能用一条SQL语句就能找出当天生日的会员?或者请大家针对小弟的程序提出一些改进的办法,谢谢!

9 个解决方案

#1


select M_Name,M_ZhengJian,M_ZJCode 
    from G_Member 
    where M_ZhengJian = '身份证' 
        and substring(M_ZJCode,len(M_ZJCode)-6,4)=substring(convert(char(8),getdate(),112),5,4)

未测试。

#2


--查询过生日的用户信息:

select M_Name,M_ZhengJian,M_ZJCode 
from G_Member 
where M_ZhengJian ='身份证' and case Len(M_ZJCode) 
when 15 then substring(M_ZJCode,9,4)
when 18 then substring(M_ZJCode,11,4)
else '' end=right(convert(varchar(8),getdate(),112),4)

#3


:(
写错了,想偷懒才发现18-6<>11。:(

#4


--1.
select * from tablename where day(会员生日字段名)=day(getdate()) and month(会员生日字段名)=month(getdate())
--2.
select * from tablename where datepart(day,会员生日字段名)=datepart(day,getdate()) and datepart(month,会员生日字段名)=datepart(month,getdate())
--3.
select * from tablename where right(convert(varchar(10),会员生日字段名),8)=right(convert(varchar(10),getdate()),8)

#5


--3.
select * from tablename where right(convert(varchar(10),会员生日字段名),5)=right(convert(varchar(10),getdate()),5)

#6


不好意思,仔细看了一下原来是身份证号字段,我的是15为,那个18为的一直仍家里没看过,参见一下left,right,substring,convert联机帮助处理一下,在跟right(convert(varchar(8),getdate(),112),6)相比较

#7


该回复被版主删除

#8


谢谢大家的热心帮助!
我参考zjcxc(邹建)朋友的方法已经成功实现,在此感谢各位。

#9


up

#1


select M_Name,M_ZhengJian,M_ZJCode 
    from G_Member 
    where M_ZhengJian = '身份证' 
        and substring(M_ZJCode,len(M_ZJCode)-6,4)=substring(convert(char(8),getdate(),112),5,4)

未测试。

#2


--查询过生日的用户信息:

select M_Name,M_ZhengJian,M_ZJCode 
from G_Member 
where M_ZhengJian ='身份证' and case Len(M_ZJCode) 
when 15 then substring(M_ZJCode,9,4)
when 18 then substring(M_ZJCode,11,4)
else '' end=right(convert(varchar(8),getdate(),112),4)

#3


:(
写错了,想偷懒才发现18-6<>11。:(

#4


--1.
select * from tablename where day(会员生日字段名)=day(getdate()) and month(会员生日字段名)=month(getdate())
--2.
select * from tablename where datepart(day,会员生日字段名)=datepart(day,getdate()) and datepart(month,会员生日字段名)=datepart(month,getdate())
--3.
select * from tablename where right(convert(varchar(10),会员生日字段名),8)=right(convert(varchar(10),getdate()),8)

#5


--3.
select * from tablename where right(convert(varchar(10),会员生日字段名),5)=right(convert(varchar(10),getdate()),5)

#6


不好意思,仔细看了一下原来是身份证号字段,我的是15为,那个18为的一直仍家里没看过,参见一下left,right,substring,convert联机帮助处理一下,在跟right(convert(varchar(8),getdate(),112),6)相比较

#7


该回复被版主删除

#8


谢谢大家的热心帮助!
我参考zjcxc(邹建)朋友的方法已经成功实现,在此感谢各位。

#9


up