On 07/03/2013 9:28 AM, Simon Slavin wrote:
On 7 Mar 2013, at 1:36pm, "Michael Black" <mdblac...@yahoo.com> wrote:

New:
select cast('2' as integer);
2
select cast('2a' as integer);
0
Sorry, but that's very bad.  There is no way that the string '2a' could 
represent 0.  I agree that interpreting '2a' as the integer 2 may be considered 
wrong, but I think you're solving the problem the wrong way.

If you want to indicate a problem, I would like to suggest that you either 
return NULL or have sqlite_step() generate an actual error code.  Of the two I 
think returning NULL is more within the spirit of SQL but I haven't though that 
through yet.

In a world where 1/0 returns NULL, it makes sense for an invalid typecast to do the same. For purposes of enforcing strong typing, a non-null integer constraint would be "check (cast(x as integer) is not null)" and a nullable integer constraint would just need to add "or x is null" -- a pretty clean solution IMO.

That does leave the question of what to do with cast ('1.0' as integer), though. Without the prefix-based matching that would now return NULL rather than 1, even though cast(1.0 as integer) would still return 1. Then again, disallowing all floats might be better than the current practice of returning 1 from a cast of both '1e-10' and '1e10' (the real->integer casts do the right thing, as does assignment to a column with integer affinity).

Ryan

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to