还是模糊查询日期问题

lcw321 2004-11-23 10:48:39
我数据库的datetime是1999-9-9格式的
如果我想查询的可能是1999或1999-9或1999-9-9
我搜过但都不和要求

sql="select * from log_time where Date(logintime) like '%"&sdate&"%' order by id desc"
的话不识别date函数

sql="select * from log_time where logintime between '"&sdate&"' and dateadd(d,1,'"&sdate&"') order by id
的话要求全部输入如1999-9-9才能查的到

select * from lgo_time where convert(varchar,logintime,20) like '%你要的值%'
的话,输入什么反应都没有了。
大家说怎么办啊?
在线等
...全文
306 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
lwlmaomao 2004-11-24
  • 打赏
  • 举报
回复
你这可以直接用sqlserver的datepart函数来完成:

例:
查询所有1999年的信息
select * from log_time where datepart(yyyy,logintime)='1999'

查询所有1999年09月的信息
select * from log_time where datepart(yyyy,logintime)='1999' and datepart(mm,logintime)='09'

查询所有1999年09月09日的信息
select * from log_time where datepart(yyyy,logintime)='1999' and datepart(mm,logintime)='09' and datepart(dd,logintime)='09'


zlj113 2004-11-24
  • 打赏
  • 举报
回复
测试数据:
create table Test_d(id int identity(1,1),date_time datetime)


insert into Test_d (date_time)


select '2004-11-10 12:21:30'

union all select '2004-11-10 12:21:30'
union all select '2004-12-10 12:21:30'
union all select '2004-11-10 12:21:30'
union all select '2004-01-10 12:21:30'
union all select '2004-11-10 12:21:30'
union all select '2004-11-10 12:21:30'
union all select '2004-11-10 12:21:30'



select * from Test_d where convert(varchar(11),date_time,121) like '%2004%'

结果为:

1 2004-11-10 12:21:30.000
2 2004-11-10 12:21:30.000
3 2004-12-10 12:21:30.000
4 2004-11-10 12:21:30.000
5 2004-01-10 12:21:30.000
6 2004-11-10 12:21:30.000
7 2004-11-10 12:21:30.000
8 2004-11-10 12:21:30.000
(所影响的行数为 8 行)


select * from Test_d where convert(varchar(11),date_time,121) like '%2004-11%'
结果:
1 2004-11-10 12:21:30.000
2 2004-11-10 12:21:30.000
4 2004-11-10 12:21:30.000
6 2004-11-10 12:21:30.000
7 2004-11-10 12:21:30.000
8 2004-11-10 12:21:30.000
(所影响的行数为 6 行)

select * from Test_d where convert(varchar(11),date_time,121) like '%2004-11-10%'
结果:
1 2004-11-10 12:21:30.000
2 2004-11-10 12:21:30.000
4 2004-11-10 12:21:30.000
6 2004-11-10 12:21:30.000
7 2004-11-10 12:21:30.000
8 2004-11-10 12:21:30.000
(所影响的行数为 6 行)


zlj113 2004-11-24
  • 打赏
  • 举报
回复
zlj113((米老鼠)抵制日货,从自己做起!) 像你那样搞了也只能查的到年


--------------------------------------------
不可能呀,我都试过了的,

像这种格式的2004-11-24就能查出24一天的记录

2004 能查2004年的记录

2004-11 能查2004年11 月份的记录,


楼主你自己认真试过没有的呀,

郁闷简直就是打击我的积极性嘛
lcw321 2004-11-24
  • 打赏
  • 举报
回复
用 convert(varchar(11),sdate,121)这个函数试试
只能符合部分要求
lcw321 2004-11-24
  • 打赏
  • 举报
回复
我的问题有点傻,大家不要见笑,外加一句,我也抵制日货
lcw321 2004-11-24
  • 打赏
  • 举报
回复

'获取表单变量,这里只过滤了空格,但在实际应用中为了安全,需要过滤更多的危险字符.
empnum=Trim(Request.Form("empnum"))
cname=Trim(Request.Form("cname"))
csex=Trim(Request.Form("csex"))
cdept=Trim(Request.Form("deptid"))
xl=Trim(Request.Form("xl"))
jg=Trim(Request.Form("jg"))
dangtuanshijian=Trim(Request.Form("dangtuanshijian"))
cstation=Trim(Request.Form("cstation"))
birth=Trim(Request.Form("birth"))
hirdate=Trim(Request.Form("hirdate"))
......................
'以下是动态生成一个sql查询字符串
'*************************************
sql="SELECT employee.empnum,employee.cname,employee.csex,employee.jcsj,department.cdept,employee.deptid,department.deptid," _
& "employee.xl,employee.jg,employee.birth,employee.hirdate,employee.jobid,employee.role,employee.mz,employee.sfz," _
......................
& " employee.stationid = station.stationid AND employee.deptid=department.deptid AND"
If empnum<>"" Then sql=sql&" empnum="&empnum&" AND"
If beyongedto<>"" Then sql=sql&" beyongedto="&beyongedto&" AND"
If cname<>"" Then sql=sql&" cname like'%"&cname&"%' AND"
If csex<>"" Then sql=sql&" csex like '%"&csex&"%' AND"
If cdept<>"" Then sql=sql&" cdept like '%"&cdept&"%' AND"
If cstation<>"" Then sql=sql&" cstation like '%"&cstation&"%' AND"
If xl<>"" Then sql=sql&" xl like '%"&xl&"%' AND"
If jg<>"" Then sql=sql&" jg like '%"&jg&"%' AND"
If dangtuanshijian<>"" Then sql=sql&" dangtuanshijian like'%"&dangtuanshijian&"%' AND"
If birth<>"" Then sql=sql&" convert(varchar(11),birth,121) like '%"&birth&"%' AND"
If hirdate<>"" Then sql=sql&" hirdate like '%"&hirdate&"%' AND"
.................
if right(sql,25)="WHERE" then sql=left(sql,clng(len(sql))-25) '解决所有参数都为空的情况
if right(sql,3)="AND" then sql=left(sql,clng(len(sql))-3) '去掉参数最后的and
sql=sql&" ORDER BY empnum "
Response.Write sql '调试语句
'**************************************

End if
%>
<form name="form1" method="post" action="">
<table width="678" border="1" align="center" cellspacing="1">
<tr>
<td colspan="2" align="center"><div align="center">查询员工</div></td>
</tr>
<tr>
<td width="6%" height="10" align="right">编号</td>
<td width="25%">
<input name="empnum" type="text" id="empnum"></td>
<td width="6%" height="10"align="right">姓名</td>
<td width="25%"><input name="cname" type="text" id="cname"> </td>
<td width="6%" align="right">性别</td>
<td width="32%"><input name="csex" type="text" id="csex"></td>
</tr>
<tr>
<td width="6%" height="10" align="right">部门</td>
<td width="25%"><input name="deptid" type="text" id="deptid"> </td>
<td width="6%" align="right">工种/职务</td>
<td width="25%">
<input name="cstation" type="text" id="cstation"></td>
<td width="6%" align="right">出生年月</td>
<td width="32%"><input name="birth" type="text" id="birth">
</td>
</tr>
<tr>
<td width="6%" height="10" align="right">参加工作时间</td>
<td width="25%"><input name="hirdate" type="text" id="hirdate"> </td>
<td width="6%" align="right">进厂时间</td>
<td width="25%"><input name="jcsj" type="text" id="jcsj"> </td>
<td width="6%" align="right">参加党团时间</td>
<td width="32%"><input name="dangtuanshijian" type="text" id="dangtuanshijian">
</td>
</tr>
**********************************************
下面是数据库中的表的结构
employee(empnum,cname,csex,.................,birth,birdate,duangtuanshijian,jcsj)
其中birth、birdate、duangtuanshijian、jcsj是datetime类型,里面的数据格式是1999-9-9
难道是表单中birth的type不应该是text类型吗?那应该是什么类型呢?
zlj113 2004-11-24
  • 打赏
  • 举报
回复
楼主你干脆把你的数据示例拿出来看看吧,以及你的sdate是什么格式的,

zlj113 2004-11-24
  • 打赏
  • 举报
回复
create table Test_d(id int identity(1,1),date_time datetime)


insert into Test_d (date_time)


select '2004-11-15'

union all select '2004-11-10'
union all select '2004-12-10'
union all select '2004-11-10'
union all select '2004-01-10'
union all select '2004-11-10'
union all select '2004-11-10'
union all select '2004-11-10'

select * from Test_d where convert(varchar(11),date_time,121) like '%2004-11-10%'

result:
11 2004-11-10 00:00:00.000
13 2004-11-10 00:00:00.000
15 2004-11-10 00:00:00.000
16 2004-11-10 00:00:00.000
17 2004-11-10 00:00:00.000



select * from Test_d where convert(varchar(11),date_time,121) like '%2004-11%'

10 2004-11-15 00:00:00.000
11 2004-11-10 00:00:00.000
13 2004-11-10 00:00:00.000
15 2004-11-10 00:00:00.000
16 2004-11-10 00:00:00.000
17 2004-11-10 00:00:00.000

select * from Test_d where convert(varchar(11),date_time,121) like '%2004%'
10 2004-11-15 00:00:00.000
11 2004-11-10 00:00:00.000
12 2004-12-10 00:00:00.000
13 2004-11-10 00:00:00.000
14 2004-01-10 00:00:00.000
15 2004-11-10 00:00:00.000
16 2004-11-10 00:00:00.000
17 2004-11-10 00:00:00.000
zlj113 2004-11-24
  • 打赏
  • 举报
回复
数据库里没有时,分秒,也行呀

lcw321 2004-11-24
  • 打赏
  • 举报
回复
to zlj113((米老鼠)抵制日货,从自己做起!很抱歉,我每说清楚
明白的说吧,我的日期是一个出生年月,数据库里面没有小时、分、秒
我的意思是 只能查的到年的比如1999,不能查到如:1999-9和1999-9-9,比如我输入1999可以查到
这一年的,但是输入1999-9和1999-9-9的时候就不行了
还有其他各位大侠的资料也很好,希望继续发扬光大!!!
小弟在此俯首百拜!!!
calmer18 2004-11-23
  • 打赏
  • 举报
回复
上面不是有答案了 楼主
lcw321 2004-11-23
  • 打赏
  • 举报
回复
我看能不能这样啊,把数据库中的数据提出来后,转换为字符串(动态的),再和客户端的数据比较
具体怎么做呢?

说的也是,也不能由的客户的要求来,不然的话,不累死我才怪
zlj113 2004-11-23
  • 打赏
  • 举报
回复
sql="select * from log_time where convert(varchar(11),logintime,121) like '%"&sdate&"%' order by id desc"

上面写错了点
yb2008 2004-11-23
  • 打赏
  • 举报
回复
日期型的数据要加#才可以的!

举个例子:


small_sale_time=cstr(request("year_year"))+"-"+cstr(request("month_month"))+"-"+cstr(request("day_day"))'日期

big_sale_time=cstr(request("year_year2"))+"-"+cstr(request("month_month2"))+"-"+cstr(request("day_day2"))

sql: out_time>=#"&small_sale_time&"# and out_time<=#"&big_sale_time&"#
zlj113 2004-11-23
  • 打赏
  • 举报
回复
sql="select * from log_time where convert(varchar(11,)logintime,121) like '%"&sdate&"%' order by id desc"

唉,N多个人问这样的问题了,楼主搜索下肯定有答案的
阿_浩 2004-11-23
  • 打赏
  • 举报
回复
like #%"&sdate&"%#
chgt 2004-11-23
  • 打赏
  • 举报
回复
你把时间分为三个部分,年、月、日,可以按自己的需要设置适当的条件进行查询!!
年是year(date),月是month(date),天是day(date)。供参考!!
sfply 2004-11-23
  • 打赏
  • 举报
回复
或者这样更简单
select * from ... where year(logintime) = '1999'
sfply 2004-11-23
  • 打赏
  • 举报
回复
换一个角度搜索嘛
比如搜索1999
你就改成
dim date_1,date_2
date_1 = cdate("1999-1-1")
date_2 = cdate("1999-12-30")

select * from log_time where logintime between "&date_1&" and "&date_2&"
yjbnew 2004-11-23
  • 打赏
  • 举报
回复
要在客户端作判断,转换的.
加载更多回复(5)

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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