doqpm82240 2015-05-26 15:45
浏览 73
已采纳

VARCHAR列值的日期比较

I have a SQL table where one of the columns 'creator' is a VARCHAR data type. That column stores values like this one: Mike Jones|05-17-2015 1:21 pm consisting of the person that created the record, the date and time it was created.

I need to use the date portion of that varchar data to form a SQL query, so that we can generate a list of tickets created between two dates. We can't restructure the table easily so i'm stuck working this out in its current format, otherwise we just would have added a CREATED date field and compared on that.

The SQL being attempted is this: SELECTticknumb,cnames,creator,ticktype,tickstatusFROMaticketsWHEREcreator>= '%05-24-2015%' ANDcreator<= '%05-30-2015%' ANDtickstatusNOT LIKE 'Closed,Complete'

Is there a way to do this directly within the SQL query without having to to a looped explode in the php first to extract and compare the earliest / latest dates?

  • 写回答

1条回答 默认 最新

  • duanqiang9212 2015-05-26 16:36
    关注

    Here is SQL snippet how you can convert your value in varchar column to date type.

    SELECT STR_TO_DATE(SUBSTRING_INDEX(`creator`, '|', -1), '%m-%d-%Y %h:%i %p')
    

    http://sqlfiddle.com/#!9/710a2/7/0

    Another inspiration

    how to convert a string to date in mysql?

    How to split the name string in mysql?

    I hope it is what you are looking for.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容