Welcome to EMC Consulting Blogs Sign in | Join | Help

Data Based

Just some thoughts of mine

2 + 2 = ?

As a concise example of the evils of type coercion I came up with the following:

INSERT INTO t
VALUES (
CAST(2 AS INT),
CAST(2 AS INT));

SELECT
CASE WHEN x + z = 4 THEN '4' ELSE 'NOT 4' END xPLUSz
FROM t;

Result:

xPLUSz
------
NOT 4

It is left as an exercise for the reader to work out what types x and z might be!

Published 26 August 2009 21:02 by David.Portas
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Luciano Evaristo Guerche (Gorše) said:

What about the following CREATE TABLE statement?

CREATE TABLE t (x varchar, z varchar);

August 27, 2009 14:56
 

Erwin Smout said:

I thought of CHAR types too.

But are you certain that coercions, by and of themselves, are the only source of the problem ?

Isn't obtuse operator overloading at least as much responsible here ?

October 8, 2010 21:50
 

Paul White said:

CREATE TABLE t (x BIT NULL, z DATETIME NULL)

August 31, 2011 12:12

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Personal Edition), by Telligent Systems