迄今为止,给定格式的 Mysql 字符串包含时区说明符 [英] Mysql string to date with given format containing a timezone specifier

查看:33
本文介绍了迄今为止,给定格式的 Mysql 字符串包含时区说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有一个字符串列

I have a string column in my database as

Wed Aug 13 17:51:06 GMT+05:30 2014

我可以将其转换为日期并在 where 子句中使用它来获取记录吗

Can i cast this to date and use this in where clause to get records

where
Timecolumn >( CURDATE()-7)

请注意时区说明符 GMT+05:30 位于时间字符串和四位数年份字符串之间.

Notice that the timezone specifier GMT+05:30 comes between the time-of-day string and the four digit year string.

我在 phpmyadmin 中运行此查询,但没有得到任何结果,是的,它们确实存在

I am running this query in phpmyadmin but i get no results and yes they do exist

 SELECT * FROM `calldetails` WHERE STR_TO_DATE('date', '%a %b %d %H:%i:%s %x %Y')>(CURDATE()-7)

日期也是我的专栏名称

推荐答案

使用 substr() 来删除时区数据.
另外 CURDATE() - 7 应该是 SUBDATE(CURDATE(), 7

Use substr() to excise the timezone data.
Also CURDATE() - 7 should be SUBDATE(CURDATE(), 7

select
    concat(substr(str, 1, 19),substr(str, 30)) as str_sans_tz,
    str_to_date(concat(substr(str, 1, 19),substr(str, 30)),
      '%a %b %e %H:%i:%s %Y') date
from mytable

输出(前后解析):

|              STR_SANS_TZ |                          DATE |
|--------------------------|-------------------------------|
| Wed Aug 13 17:51:06 2014 | August, 13 2014 17:51:06+0000 |

SQLFiddle

应用于您的查询:

SELECT * FROM `calldetails`
WHERE str_to_date(concat(substr(`date`, 1, 19), substr(`date`, 30)), '%a %b %e %H:%i:%s %Y') > SUBDATE(CURDATE(), 7)

这篇关于迄今为止,给定格式的 Mysql 字符串包含时区说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆