Data Truncation: Truncated Incorrect DOUBLE Value When Updating Timestamp

Posted February 18, 2009 at 3:04 PM

Tags: ColdFusion, SQL

I just spent the last hour trying to debug the smallest SQL problem! I have a datatable of Contracts and I was building a feature in our client software where we could end all contracts of a certain type (SLA) at the same time (trust me, there is a business reason behind this). To do this, I was executing the following MySQL Cross-Table UPDATE statement:

 Launch code in new window » Download code as text file »

  • UPDATE
  • contract c
  • INNER JOIN
  • contract_type t
  • ON
  • (
  • t.reference_key = 'SLA'
  • AND
  • c.contract_type_id = t.id
  • )
  • SET
  • c.date_updated = <cfqueryparam value="#Now()#" cfsqltype="cf_sql_timestamp" />
  • AND
  • c.date_ended = <cfqueryparam value="#Now()#" cfsqltype="cf_sql_timestamp" />
  • WHERE
  • c.date_ended IS NULL

I'm trying to set the date_ended field to Now() where ever it is currently NULL and of the correct contract type (SLA). However, when I ran this query, MySQL kept throwing this error:

Data truncation: Truncated incorrect DOUBLE value: '2009-02-18 13:43:35'

The error certainly wants you to believe that this is a data problem. Specifically, the error wants you to believe that the date/time value you are using in the query is somehow being converted to a double and that the converted value is too big for a double. So naturally, that's what I was trying to debug. But, after a solid hour, no joke, I came to realize that this error has nothing to do with data at all and is, in fact, a syntax error!

Look at my SET statement:

 Launch code in new window » Download code as text file »

  • SET
  • c.date_updated = <cfqueryparam value="#Now()#" cfsqltype="cf_sql_timestamp" />
  • AND
  • c.date_ended = <cfqueryparam value="#Now()#" cfsqltype="cf_sql_timestamp" />

See the problem now? I have it written out like a WHERE statement; a proper SET statement uses commas, not ANDs. Rewritten properly, the query is:

 Launch code in new window » Download code as text file »

  • UPDATE
  • contract c
  • INNER JOIN
  • contract_type t
  • ON
  • (
  • t.reference_key = 'SLA'
  • AND
  • c.contract_type_id = t.id
  • )
  • SET
  • c.date_updated = <cfqueryparam value="#Now()#" cfsqltype="cf_sql_timestamp" />,
  • c.date_ended = <cfqueryparam value="#Now()#" cfsqltype="cf_sql_timestamp" />
  • WHERE
  • c.date_ended IS NULL

So yeah, that's an hour of my life I won't get back. That's a really poor error that MySQL is throwing. Not that I'm blaming MySQL - I was the one who wrote the crappy statement - but, it did sort of lead me on a wild goose chase. Next time, thought, I'll be prepared!

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Reader Comments

Feb 18, 2009 at 3:20 PM // reply »
39 Comments

Good thing that you have your own company. If you worked for someone they might have ripped you for "wasting" an hour.

Those bugs are so much fun.


Feb 18, 2009 at 3:44 PM // reply »
6,516 Comments

@Brandon,

Hey, I was still doing client work :)


Feb 18, 2009 at 9:59 PM // reply »
27 Comments

Just curious. Why did you write out:
c.date_ended = <cfqueryparam value="#Now()#" cfsqltype="cf_sql_timestamp" />
When MySQL has a built in "now()" function. Would it be cleaner (and a mirco-fraction faster) if you did this:
c.date_ended = now()

I imagine you have your reasons.
(Like:
To keep the SQL cross DB compliant
DB server isn't in same timezone as Webserver
)


Feb 19, 2009 at 7:55 AM // reply »
6,516 Comments

@Tim,

I just use it out of habit. I like to keep the SQL as consistent as possible and so I use CFQueryParam for ally my dynamic value. Plus, sometimes I have dates that have to be null based on other values and CFQueryParam makes that wicked easy:

<cfqueryparam value="#FORM.date_started#" type="cf_sql_timestamp" null="#(NOT IsDate( FORM.date_started ))#" />

With this, I can put my null-ify logic right into the CFQueryParam tag and keep my SQL free of CFIF statements regarding NULL inserts.


Mar 19, 2009 at 9:52 PM // reply »
1 Comments

WOW. I just did the same thing! I actually spent a couple hours trying to figure this out :(


Mar 23, 2009 at 8:31 AM // reply »
6,516 Comments

@Chuck,

Yeah, the error message is SOOOO misleading!


May 13, 2009 at 1:41 AM // reply »
1 Comments

Whahaha. You just saved me an hour ;)


May 13, 2009 at 7:23 AM // reply »
6,516 Comments

@Jrgns,

No problem my man :) It's a horribly unhelpful error message, so glad to help.


Jun 1, 2009 at 5:19 AM // reply »
1 Comments

GOD!

same mistake here.

the error message REALLY HELPED(in making debugging more complex I mean:P)


Jun 7, 2009 at 10:40 AM // reply »
1 Comments

Thanks for that one Ben :)


Jul 1, 2009 at 3:11 PM // reply »
1 Comments

Wow, you just saved me an hour. Thanks.


Jul 27, 2009 at 3:37 AM // reply »
1 Comments

Thanks man, saved me an hour for sure...will remember this one for a long time.


Aug 21, 2009 at 3:17 AM // reply »
1 Comments

Thanks very much!Actually you saved me an hour.and thanks you one hour.


Sep 6, 2009 at 1:38 PM // reply »
6,516 Comments

Glad to help out guys.


Nov 9, 2009 at 11:30 AM // reply »
1 Comments

Just saved me a morning of frustration too!

Thanks!


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »