日期查询格式(字符)识别问题

itzhiren 2003-12-05 09:27:00
Select Case cmbproject.Text
Case "生日"
priv_project = "birthdate"
End Select
sql = "select client.* from client where client." & "" & priv_project & " like '%" & txtcondition.Text & "%' order by client.idclient"


我用这个查询来查询纪录,但是当priv_project 为日期型的字段时,不识别“-”这个字符,比如说我要查2003-07-17日的记录,我输入2、0、3、1、7、2003、17都可以查出来,但是输入2003-07-17就查不出来了,请问这是什么原因?

...全文
84 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenlishu413 2003-12-07
  • 打赏
  • 举报
回复
回复人: itzhiren(itzhiren) ( ) 信誉:100 2003-12-05 10:04:00 得分:0


但是客户要求yyyy-mm-dd的形式


你可以在計算的時候才
format(字符串,"yyyymmdd")
SoHo_Andy 2003-12-05
  • 打赏
  • 举报
回复
datetime 字段的长度是不能改的,是内置的数据类型,和字符串长度的理解不能混淆

如果你一定要在文本框输入,则就最好先检查并格式化用户输入的内容
itzhiren 2003-12-05
  • 打赏
  • 举报
回复
可是我的datetime类型字段长度是8,可是在文本框中输入时,输到yyyy-mm-长度已经是8了,所以会出现char类型向datetime类型转换出现语法错误,怎样才能更改datetime类型字段长度?
somecom 2003-12-05
  • 打赏
  • 举报
回复
最保险就是先格式化日期,使其标准化。
date1 = Format(txtcondition.Text, "yyyy-mm-dd")
然后再引用它
SoHo_Andy 2003-12-05
  • 打赏
  • 举报
回复
你这个错误产生是你这种实现方法的缺陷,要想避免最好使用其它方法
比如
1、使用DTPicker控件,它是选取日期的经典控件,按照需求没有必要用户按一个键就
查找一次记录集吧
2、使用语法检查即转换,即在调用数据库的查找之前写函数对用户输入的部分进行语义等基本的检查和简单转换,这样可以减少数据库操作错误

Timer事件通常不这样使用的
itzhiren 2003-12-05
  • 打赏
  • 举报
回复
Private Sub txtcondition_Change()
Timer1_Timer
End Sub
Private Sub Timer1_Timer()
If cmbproject.Text = "" Then
MsgBox "请选择查询项目!"
Exit Sub
End If
On Error Resume Next
Select Case cmbproject.Text
Case "出生日期"
sql = "select client.*,clienttype.clienttype,subpart.subname from client,clienttype,subpart where client.idsubpart=subpart.idsubpart and client.idclienttype=clienttype.idclienttype and client.birthdate = '" & Format(txtcondition.Text, "yyyy-mm-dd") & "' order by client.idclient"
End Select
Adodc1.RecordSource = sql
Adodc1.Refresh

Set rs = Adodc1.Recordset

Set DataGrid1.DataSource = rs
DataGrid1.Refresh
Label29.Caption = "共查出" & rs.RecordCount & "条记录"
SoHo_Andy 2003-12-05
  • 打赏
  • 举报
回复
你是在什么地方检测输入的,keypress么
贴完整点的代码出来
itzhiren 2003-12-05
  • 打赏
  • 举报
回复
sql = "select client.*,clienttype.clienttype,subpart.subname from client,clienttype,subpart where client.idsubpart=subpart.idsubpart and client.idclienttype=clienttype.idclienttype and client.birthdate = '" & Format(txtcondition.Text, "yyyy-mm-dd") & "' order by client.idclient"

我写成这样,运行的时候,假设要输入2003-12-3,在输到2003-12-的时候,会出现一个错误:从字符串转换为datetime时发生语法错误

因为我的前面的代码有on error resume next,所以可以继续把日期输入完整,输入完整以后,记录就可以查出来了,所以我想把这个错误屏蔽掉,请问怎样屏蔽掉?
itzhiren 2003-12-05
  • 打赏
  • 举报
回复
数据库中日期没有问题。
rexyudl 2003-12-05
  • 打赏
  • 举报
回复
看看源头吧!
是不是你存到数据库的时候,日期出了问题!
SoHo_Andy 2003-12-05
  • 打赏
  • 举报
回复
试试这个,应该可以了

sql = "select client.* from client where client." & "" & priv_project & " = #'" & format(txtcondition.Text,"yyyy-mm-dd") & "'# order by client.idclient"
rexyudl 2003-12-05
  • 打赏
  • 举报
回复
你存到数据库里是什么格式啊?
怎么存的啊?
取的时候就怎么取,判断的时候也要严格按照你保存的时候的格式来!
itzhiren 2003-12-05
  • 打赏
  • 举报
回复
还是会出现“从字符串转换为datetime时发生语法错误”,怎样屏蔽掉这个错误,不让它出现呢?
SoHo_Andy 2003-12-05
  • 打赏
  • 举报
回复
sql = "select client.* from client where client." & "" & priv_project & " = '" & format(txtcondition.Text,"yyyy-mm-dd") & "' order by client.idclient"
itzhiren 2003-12-05
  • 打赏
  • 举报
回复
回复人: SoHo_Andy(冰) ( ) 信誉:100 2003-12-05 09:32:00 得分:0 很少见对日期型字段使用 like 语句的,可能SQL本身不推荐,所以这种类型字段对
like 语句有bug吧


如果不用like也可以,用=
即sql = "select client.* from client where client." & "" & priv_project & " = '" & txtcondition.Text & "' order by client.idclient"

这样的话,会出现“从字符串转换为datetime时发生语法错误”,怎样屏蔽掉这个错误,不让它出现呢?
itzhiren 2003-12-05
  • 打赏
  • 举报
回复
但是客户要求yyyy-mm-dd的形式
chenlishu413 2003-12-05
  • 打赏
  • 举报
回复
format(字符串,"yyyymmdd")
這樣子就可以去掉-
itzhiren 2003-12-05
  • 打赏
  • 举报
回复
不对,如果用空格代替-,还是查不出来
northwolves 2003-12-05
  • 打赏
  • 举报
回复
try:
sql = "select client.* from client where client." & "" & priv_project & " like '%" & replace(txtcondition.Text,"-","") & "%' order by client.idclient"
SoHo_Andy 2003-12-05
  • 打赏
  • 举报
回复
很少见对日期型字段使用 like 语句的,可能SQL本身不推荐,所以这种类型字段对
like 语句有bug吧
加载更多回复(2)

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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