【问题标题】:Date Datatype working other datatype not working Sql日期数据类型工作其他数据类型不工作Sql
【发布时间】:2015-03-25 18:33:22
【问题描述】:

当我选择使用date 格式时,它正在工作,例如:

select * 
from driverdetails 
where licenserenewaldate between '2015-01-27' and '2015-02-27'

当我使用varchartext (27-01-2015) 时,它不会检索。

select * 
from driverdetails 
where licenserenewaldate between '27-01-2015' and '27-02-2015'

它不工作。它显示的是空值。

【问题讨论】:

  • 日期格式默认为yyyy-mm-dd。检查格式。

标签: php mysql sql-server codeigniter


【解决方案1】:

如果您使用 POST 或 GET 请求提交要过滤的数据,您可以先使用 strtotime() 函数,然后再将其包含在查询中。

看看这个strtotime manual

格式化yyyy-mm-dd中的数据:

$yourDateData=date('Y-m-d',strtotime($yourDateData));

更多日期格式可以参考here

【讨论】:

  • 是的,谢谢它正在工作,有没有可能从不同的数据类型中获取......
  • @selva - 有哪些不同的数据类型?能举个例子吗?
【解决方案2】:

你应该使用下面的,我希望你会得到你想要的结果

select * 
from driverdetails 
where licenserenewaldate between str_to_date('27-01-2015','%d-%m-%Y') AND 
str_to_date('27-02-2015','%d-%m-%Y')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多