Data Truncation: Truncated Incorrect DOUBLE Value When Updating Timestamp

Posted February 18, 2009 at 3:04 PM by Ben Nadel

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:

  • 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:

  • 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:

  • 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!



Reader Comments

Feb 18, 2009 at 3:20 PM // reply »
42 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 »
11,238 Comments

@Brandon,

Hey, I was still doing client work :)


Feb 18, 2009 at 9:59 PM // reply »
39 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 »
11,238 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 »
11,238 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 »
11,238 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 »
11,238 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!


Feb 16, 2010 at 11:52 AM // reply »
5 Comments

MySQL raised same crappy error on the following syntax:

select IFNULL(id,0)+1 into nextval from nextids where UPPER(tablename)=UPPER('''' + tbname + '''')

The correct syntax should be:

select IFNULL(id,0)+1 into nextval from nextids where UPPER(tablename)=UPPER(CONCAT('''',tbname,''''));

Thank you,Ben. It is your post that enlightened me to stop searching for errors in places where there aren't chances to find one :))


Feb 17, 2010 at 10:16 PM // reply »
11,238 Comments

@Lucian,

Going from MS SQL to MySQL, it took me a while to get in the mindset of CONCAT(). In MS SQL, using the "+" is so easy.


Feb 18, 2010 at 4:36 PM // reply »
1 Comments

You wasted an hour of your life but saved me one - thanks for posting this!


Feb 18, 2010 at 4:55 PM // reply »
5 Comments

@Ben

Yes, Ben, couldn't they just have used that tiny "+" sign? :))

Oh, another thing!
After using the concat, i kept receiving this error: "No data fetched and-some-other-thing-bla-bla" Said to myself: "Bugger! What could be wrong this time?"

Well, since I am a Delphi developer, at my origins, i always add the quotes when comparing a constant-value string to a variable string/a string-type parameter.
Well (again), it seems that MySQL doesn't need the quotes, it has it's own. Very ashamed of not knowing this, stop "boo"-ing me.

So, finally, the winning syntax was:

select IFNULL(id,0)+1 into nextval from nextids where UPPER(tablename)=UPPER(tbname)

It was right there, in front of me, all the time :)

Nice night to you, Ben, and thanks again.

Lucian


Feb 22, 2010 at 9:09 PM // reply »
11,238 Comments

@Lucian,

Sometimes, error just seem to do more harm than good :) They almost act as misdirection.


Mar 5, 2010 at 10:50 AM // reply »
1 Comments

Saved me the hour! Thanks!


Mar 8, 2010 at 7:10 PM // reply »
11,238 Comments

@JGreen,

My pleasure!


Apr 16, 2010 at 10:31 PM // reply »
1 Comments

Similar issue: appear to have a problem with a double, but that is not the cause....

create table test (col1 varchar(10), col2 varchar(10), col3 double);

insert into test values ("a","b",0);
update test set col3=1.2 where col1+col2="a"+"b";

Query OK, 1 row affected, 2 warnings (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

show warnings;
+---------+------+---------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'a' |
| Warning | 1292 | Truncated incorrect DOUBLE value: 'b' |
+---------+------+---------------------------------------+

update test set col3=1.2 where concat(col1,col2)="a"+"b";

Query OK, 0 rows affected, 2 warnings (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0

To work without warnings,

update test set col3=1.2 where col1+col2=concat("a","b");

or

update test set col3=1.2 where concat(col1,col2)=concat("a","b");


Apr 19, 2010 at 9:38 PM // reply »
11,238 Comments

@Adam,

Not that this is related to the problem at all, but don't you need single quotes when dealing with SQL string values?


Apr 20, 2010 at 2:37 AM // reply »
5 Comments

@Ben:

No, double quotes work as well. He just has to pay attention to the letter case.

Greetings,
Lucian


Apr 21, 2010 at 9:44 AM // reply »
11,238 Comments

@Lucian,

Really? I had no idea. Is this true for all SQL? Or just some databases?


Apr 21, 2010 at 10:15 AM // reply »
5 Comments

@Ben
Weeeeelll ... it did apply to MySQL and SQL Server and Firebird.

Can't be sure about Interbase, though, it's been a long time.

Lucian


Apr 21, 2010 at 10:49 AM // reply »
11,238 Comments

@Lucian,

Very cool; I'll have to give this a try.


Apr 22, 2010 at 1:58 AM // reply »
5 Comments

@Ben

Keep me posted. Who knows? Some things might have changed.

Lucian


Jun 24, 2010 at 4:42 PM // reply »
1 Comments

Same error in 5.0.67 :(
oh well thanks for saving my debugging time.


Jul 4, 2010 at 7:53 PM // reply »
4 Comments

Hey mate,

Thanks to Google and your blog I now know why I have been getting this error for the past 3 months. Now I don't have to update fields one by one like a sucker.

Nate
Adelaide, Australia


Aug 1, 2010 at 10:24 PM // reply »
11,238 Comments

@Nate,

Awesome my man. Glad this was able to help you sort out the problems.


ari
Aug 26, 2010 at 7:14 PM // reply »
1 Comments

thanks!


Oct 8, 2010 at 7:33 AM // reply »
1 Comments

Ben, you just saved me a lot of time. Thanks!


Oct 9, 2010 at 11:05 AM // reply »
1 Comments

Nice work, Ben. I can really count on you. Seriously.


Oct 10, 2010 at 3:27 PM // reply »
11,238 Comments

@Ari, @Hrish, @Mike,

Always happy to help. Glad this post helped you narrow down your problem.


Jan 5, 2011 at 4:51 PM // reply »
1 Comments

Thanks a loads man!!
hw silly on my part


Mar 11, 2011 at 2:04 PM // reply »
1 Comments

you rock!!


Apr 10, 2011 at 4:49 PM // reply »
1 Comments

Commas, not ANDs in MySQL UPDATE statements. Just what I needed - thanks for posting those comments - you rock.


Jun 8, 2011 at 1:07 AM // reply »
1 Comments

Awesome, saved my time.


Sep 9, 2011 at 2:11 AM // reply »
1 Comments

Small but very typical syntax error.
U saved my hour.
Thanks.


Oct 2, 2011 at 11:40 PM // reply »
1 Comments

Well FWIW you saved ME an hour tonight.

THANKS!


Oct 3, 2011 at 3:55 PM // reply »
1 Comments

Thaks for your hour man!!!!


Apr 19, 2012 at 12:53 PM // reply »
1 Comments

I have created a procedure in mysql to create a dynamic table ,although the procedure is prepared successfully but by a call myp(args);
Its giving an error:Truncated incorrect Double value:'CREATE TABLE'
The code is written below
please help !

delimiter//
Create Procedure myp
(IN t_name VARCHAR(25) )

Begin
declare sqlString VARCHAR(150) ;

declare tableName VARCHAR(30);
set tableName :=t_name;
set sqlString :='CREATE TABLE '|| tableName || '(order_id VARCHAR NOT NULL, item_id VARCHAR NOT NULL)';

End;
//


Apr 29, 2012 at 1:23 PM // reply »
1 Comments

Two years later and still the same crappy error message.

Thanks for this, it saved me a headache.


Jun 15, 2012 at 5:21 AM // reply »
1 Comments

LOL i've got the same problem. thanks dude


Jun 20, 2012 at 4:11 AM // reply »
1 Comments

Thanks. saved one hour :)


Jul 23, 2012 at 2:45 AM // reply »
1 Comments

you're awesome man!haha this error gave me trouble also..thanks a lot! \m/


Jay
Oct 23, 2012 at 5:30 PM // reply »
1 Comments

Thanks! I owe you one.


Nov 16, 2012 at 5:18 AM // reply »
1 Comments

Your hour saved up lots of other developer's hours! Thanks :)


Feb 4, 2013 at 5:35 AM // reply »
1 Comments

Thank you.



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2013 at 7:46 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
No luck. At least I have uncovered the cause, URLScan 3.1. Here is what I see in the IIS log when a file is over 30mb. 2013-05-21 23:29:05 10.105.45.128 GET /plupload/assets/jquery/jquery-1.8. ... read »
May 21, 2013 at 6:12 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
Ben, I did not see you after Pete Freitag's Lockdown session at cfObjective but he said that IIS sets file size limits at 30MB by default which just happened to be the threshold for file size when ... read »
May 21, 2013 at 11:51 AM
Ask Ben: Parsing Very Large XML Documents In ColdFusion
Looking at my first ever XML document that I have to parse and put into MS SQL 2000 with CF8. I get it to list the desired Field name, many times over, and have a long list of this field name displa ... read »
May 21, 2013 at 9:25 AM
Turning Off and On Identity Column in SQL Server
you are awesome..i am lucky to get this blog between such a garbage one....Thanks, Prashant ... read »
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools