Geeks With Blogs

News hi and welcome my name is edo van vliet, i'm a freelance microsoft developer and overall geek (and proud of it :)
readme.txt a geek. now armed with a blog.
was working with a quick query to find some data in a netxt field containing a comma-seperated list of countries, so i wrote:
select id, countries from tablea where cast(countries as varchar) LIKE '%mozambique%'
this didn't return the number of rows i expected so i investigated further. it seems that using cast() in this way (without specifiying  a length) creates a varchar of default length. if a string in my ntext fields is longer than this length thestring is terminated. hmm, fun.
my curiosity got the better of my so i checked to see what the default length actually is.
select len(cast(countries as varchar)), datalength(cast(countries as varchar)), countries 
from tablea
returns 30 for both len() and datalenght(). 30? why 30?? what an decidly odd default.


[update] thanks to my friends at stackoverflow i now see the official ms doc stating that the default is 30. I still wonder about the why though...
Posted on Thursday, December 11, 2008 1:28 PM SQL | Back to top

Comments on this post: sql issue with cast

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © edoode | Powered by: