Domanda

THE SITUATION I pull timestamps formatted in RFC 2822 (Sat, 01 Dec 2012 05:49:45 +0000) and store them in a VARCHAR field in MYSQL. I have a start_date and end_date.

THE GOAL Search BETWEEN two dates (like start_date BETWEEN '2012-11-01' AND '2012-12-01')

THE CONDITIONS I want to do this with pure SQL and not do post processing in PHP

THE ACCEPTABLE COMPROMISE I don't want to, but I will convert and store them as DATETIME by using PHP if needed.

Can anyone help me accomplish my goal (listed above). Rick

È stato utile?

Soluzione

You could convert your string dates do datetime using str_to_date:

select str_to_date('Sat, 01 Dec 2012 05:49:45 +0000','%a, %d %b %Y %T')

If you need to convert also timezone, try this:

set @datestring='Sat, 01 Dec 2012 05:49:45 +0000';
select
  CONVERT_TZ(
    str_to_date(@datestring,'%a, %d %b %Y %T'),
    concat(mid(@datestring, 27, 3), ':', mid(@datestring, 30, 2)),
    '+00:00'
  )

Altri suggerimenti

Store them as a native DATETIME. This is the only sane approach.

Why are you so opposed to using the proper tool for the job?

Storing timestamps as a string is poor use of the database features. Since they were all the same format, they could have easily been converted to a datetime on input.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top