<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: MySQL datetime insert</title>
	<link>http://ask.metafilter.com/113485/MySQL-datetime-insert/</link>
	<description>Comments on Ask MetaFilter post MySQL datetime insert</description>
	<pubDate>Thu, 05 Feb 2009 14:34:06 -0800</pubDate>
	<lastBuildDate>Thu, 05 Feb 2009 14:34:06 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: MySQL datetime insert</title>
		<link>http://ask.metafilter.com/113485/MySQL-datetime-insert</link>	
		<description>Inserting a date into MySQL?  Is there a to_date() equivalent to convert a string to a datetime? &lt;br /&gt;&lt;br /&gt; I can do this in Oracle:&lt;br&gt;
&lt;br&gt;
    insert into table_name&lt;br&gt;
        (date_field)&lt;br&gt;
    values &lt;br&gt;
	(to_date(&apos;05/03/2003 21:02:44&apos;, &apos;mm/dd/yyyy hh24:mi:ss&apos;));&lt;br&gt;
&lt;br&gt;
Is there a similar to_date() function in MySQL that will let me format a string?  Everything I&apos;m finding on the web says that the only way to do it is if your string is &lt;em&gt;already&lt;/em&gt; in &apos;YYYY-MM-DD HH:MM:SS&apos; format (that&apos;s from the &lt;a href=&quot;http://dev.mysql.com/doc/refman/5.1/en/datetime.html&quot;&gt;manual&lt;/a&gt;)&lt;br&gt;
&lt;br&gt;
I have a few hundred records in a text file that I need load into a table, so I can&apos;t use tricks like now() and I really don&apos;t want to have to fix a all those dates.  Ideas?  Suggestions?</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2009:site.113485</guid>
		<pubDate>Thu, 05 Feb 2009 14:20:33 -0800</pubDate>
		<dc:creator>exhilaration</dc:creator>
		
			<category>mysql</category>
		
			<category>oracle</category>
		
			<category>datetime</category>
		
			<category>insert</category>
		
	</item> <item>
		<title>By: DecemberBoy</title>
		<link>http://ask.metafilter.com/113485/MySQL-datetime-insert#1630303</link>	
		<description>I&apos;m pretty sure it has to be in MySQL&apos;s preferred format. I would write a quickie Python script to convert the dates to the right format and insert them into the database. Something like:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;br&gt;
for line in open(&apos;thefile.txt&apos;,&apos;r&apos;):&lt;br&gt;
    #(split the line as needed to isolate the date)&lt;br&gt;
    od = strptime(orig_date,orig_format)&lt;br&gt;
    nd = od.strftime(new_format)&lt;br&gt;
    #Put MySQL insert stuff here&lt;br&gt;
&lt;/pre&gt;&lt;br&gt;
&lt;br&gt;
If you don&apos;t know Python, then pretty much every scripting language has strftime and strptime. &lt;a href=&quot;http://docs.python.org/library/time.html&quot;&gt;Here&apos;s&lt;/a&gt; the documentation for the formatting, so for example if you needed to convert from MM/DD/YY to DD-MM-YY, orig_format would be &quot;%m/%d/%y&quot; and new_format would be &quot;%d-%m-%y&quot;. Again, that formatting is the same in other languages.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2009:site.113485-1630303</guid>
		<pubDate>Thu, 05 Feb 2009 14:34:06 -0800</pubDate>
		<dc:creator>DecemberBoy</dc:creator>
	</item><item>
		<title>By: orthogonality</title>
		<link>http://ask.metafilter.com/113485/MySQL-datetime-insert#1630304</link>	
		<description>SELECT STR_TO_DATE(&apos;04/31/2004&apos;, &apos;%m/%d/%Y&apos;);&lt;br&gt;
        -&amp;gt; &apos;2004-04-31&apos;&lt;br&gt;
&lt;br&gt;
Since this is MySQL, check for warnings and select min and max values for a sanity check -- depending on your settings, it will cheerfully insert bullshit given bad or ambiguous input. Note also that the function&apos;s return value can be of type date, time, or datetime, dependingon inut.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2009:site.113485-1630304</guid>
		<pubDate>Thu, 05 Feb 2009 14:34:41 -0800</pubDate>
		<dc:creator>orthogonality</dc:creator>
	</item><item>
		<title>By: McGuillicuddy</title>
		<link>http://ask.metafilter.com/113485/MySQL-datetime-insert#1630358</link>	
		<description>orthogonality&apos;s suggestion is good provided you can lose the time part of the &quot;datetime&quot;. You also might consider using a &lt;a href=&quot;http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_unix-timestamp&quot;&gt;UNIX_TIMESTAMP&lt;/a&gt; field for ease of loading and converting from there. Here is the list of&lt;a href=&quot;http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html&quot;&gt; MySQL 5.1 date and time functions&lt;/a&gt;.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2009:site.113485-1630358</guid>
		<pubDate>Thu, 05 Feb 2009 15:12:08 -0800</pubDate>
		<dc:creator>McGuillicuddy</dc:creator>
	</item><item>
		<title>By: McGuillicuddy</title>
		<link>http://ask.metafilter.com/113485/MySQL-datetime-insert#1630404</link>	
		<description>Oh yeah, STR_TO_DATE can retain time. &lt;br&gt;
&lt;br&gt;
&amp;gt; select str_to_date(&apos;05/03/2003 21:02:44&apos;, &apos;%c/%d/%Y %T:%i:%f&apos;)  ;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2009:site.113485-1630404</guid>
		<pubDate>Thu, 05 Feb 2009 15:44:03 -0800</pubDate>
		<dc:creator>McGuillicuddy</dc:creator>
	</item>
	</channel>
</rss>
