Mysql日期格式转换

时间:2014-07-26 09:16:10

标签: mysql sql string date squid

我想从[09/Jul/2014:16:12:58 +0530]子字符串日期,并希望将其提供给日期类型字段。 我的完整查询就像这样

    insert into SquidLog 
   (LocalIP,AccessDateTime,Url,HttpAccessCode,BytesAccessed,DestinationIP,
   SquidErrorCode,SquidTimeStamp,AccessDate,AccessTime) select LocalIP,
   AccessDateTime,Url,HttpAccessCode,BytesAccessed,DestinationIP,
   SquidErrorCode,SquidTimeStamp, 
   DATE_FORMAT(replace(SUBSTR(AccessDateTime,2,11),"/","-"),'%Y-%b-%d'), 
   SUBSTR(AccessDateTime,14,8) from SquidLogTemp;

怎么可能。

2 个答案:

答案 0 :(得分:0)

试试这个:

insert into SquidLog 
   (LocalIP,AccessDateTime,Url,HttpAccessCode,BytesAccessed,DestinationIP,
   SquidErrorCode,SquidTimeStamp,AccessDate,AccessTime) select LocalIP,
   AccessDateTime,Url,HttpAccessCode,BytesAccessed,DestinationIP,
   SquidErrorCode,SquidTimeStamp, 
   STR_TO_DATE(replace(SUBSTR(AccessDateTime,2,11),"/","-"),'%Y-%b-%d'), 
   SUBSTR(AccessDateTime,14,8) from SquidLogTemp;

答案 1 :(得分:0)

DATE_FORMAT(STR_TO_DATE(SUBSTR(AccessDateTime,2,11), '%d /%B /%Y'), '%Y-%B-%d')

    CREATE TABLE SquidLogTemp
        (`AccessDateTime` varchar(26))
    ;

    INSERT INTO SquidLogTemp
        (`AccessDateTime`)
    VALUES
        ('09/Jul/2014:16:12:58 +0530')
    ;

**Query 1**:

    select
           date_format(str_to_date(AccessDateTime,'%d/%b/%Y'),'%Y-%b-%d')
         , substring(AccessDateTime,13,8)
         , str_to_date(substring(AccessDateTime,13,8),'%H:%i:%s') 
    from SquidLogTemp



**[Results][2]**:

    | DATE_FORMAT(STR_TO_DATE(ACCESSDATETIME,'%D/%B/%Y'),'%Y-%B-%D') | SUBSTRING(ACCESSDATETIME,13,8) | STR_TO_DATE(SUBSTRING(ACCESSDATETIME,13,8),'%H:%I:%S') |
    |----------------------------------------------------------------|--------------------------------|--------------------------------------------------------|
    |                                                    2014-Jul-09 |                       16:12:58 |                         January, 01 1970 16:12:58+0000 |

http://sqlfiddle.com/#!9/a24ce/1